cjbhaines / Log4Net.Async

Asynchronous Log4Net appenders and forwarder
http://www.nuget.org/packages/Log4Net.Async/
MIT License
121 stars 37 forks source link

HttpContext.Current not preserved #2

Closed rcollette closed 9 years ago

rcollette commented 9 years ago

Should be resolvable by implementing StartForward as:

    private void StartForwarding()
    {
        if (!this.shutDownRequested)
        {
            HttpContext ctx = HttpContext.Current;
            Thread thread = new Thread(new ThreadStart(() =>
            {
                HttpContext.Current = ctx;
                this.ForwardingThreadExecute();
            }))
            {
                Name = string.Format("{0} Forwarding Appender Thread", base.Name),
                IsBackground = false
            };
            this.forwardingThread = thread;
            this.forwardingThread.Start();
        }
    }
rcollette commented 9 years ago

On second thought. This only captures the HttpContext of the first request. The context would have to be captured in the LoggingEvent object. Darn.

cjbhaines commented 9 years ago

Hi Richard,

Also referencing the HttpContext puts a dependency on System.Web which I think is a bad idea. Can you think of a way to do this with an extension point of some kind instead?

Cheers, Chris

rcollette commented 9 years ago

HttpContext may be get and set using CallContext as an object, so no dependencies on System.Web. However, log4net PatternLayout already has a dependency on System.Web so I'm not sure it's a big deal in any case. While accessing it in this manner amounts to relying on the equivalent of an "internal" call, the likelihood this will ever change without HttpContext.Current itself being deprecated is going to be very slim. I'm generating a pull request now. It's a very minor internal change to support this.

cstigas commented 9 years ago

Just curious if this has been committed. I got the latest version (1.1.0) and the HttpContext.Current is always null.

rcollette commented 9 years ago

It was committed and I know it was working in other code at work. But at home, in VS2013 on Windows 7, I'm getting null for the value retrieved using CallContext. Unfortunately, I don't have reflector at home to dig into HttpContext.Current. However, CallContext.HostContext does contain the current HttpContext but I'm getting an error in the forwarding loop after setting CallContext.HostContext

log4net:ERROR [TraceAppender] ErrorCode: GenericFailure. Failed in DoAppend System.NullReferenceException: Object reference not set to an instance of an object. at System.Web.Hosting.IIS7WorkerRequest.GetQueryStringRawBytes() at System.Web.HttpRequest.FillInQueryStringCollection() at System.Web.HttpRequest.EnsureQueryString() at System.Web.HttpRequest.get_QueryString() at System.Web.HttpRequest.FillInParamsCollection() at System.Web.HttpRequest.GetParams() at log4net.Layout.Pattern.AspNetRequestPatternConverter.Convert(TextWriter writer, LoggingEvent loggingEvent, HttpContext httpContext) at log4net.Layout.Pattern.PatternLayoutConverter.Convert(TextWriter writer, Object state) at log4net.Util.PatternConverter.Format(TextWriter writer, Object state) at log4net.Layout.PatternLayout.Format(TextWriter writer, LoggingEvent loggingEvent) at log4net.Appender.AppenderSkeleton.RenderLoggingEvent(TextWriter writer, LoggingEvent loggingEvent) at log4net.Appender.AppenderSkeleton.RenderLoggingEvent(LoggingEvent loggingEvent) at log4net.Appender.TraceAppender.Append(LoggingEvent loggingEvent) at log4net.Appender.AppenderSkeleton.DoAppend(LoggingEvent loggingEvent)

I'll have to look at the .NET source on Friday to determine what is missing.

rcollette commented 9 years ago

Just pinging to let you know I haven't forgotten this. Was away on holiday over Thanksgiving.

rcollette commented 9 years ago

@cjbhaines - Any chance you can push this to nuget? A guy at my old company is using a custom build but now this should work for him.

cjbhaines commented 9 years ago

I just pushed it. Sorry this took so long!

rcollette commented 9 years ago

Not a problem. I just switched jobs myself and have zero time to spare. I know how it goes. I've working on another open source project for more than a year and it's still not ready to release. OS is not an easy job.

cjbhaines commented 9 years ago

I just took on a new role as well so know how you feel! Good luck with the new job. I made you a contributor so you can now freely commit. I think that should also give you permission to deploy to NuGet from AppVeyor.