Question

I have a library assembly that outputs trace information, and a client winforms application that adds a trace listener via app.config. If I were to use the library in ASP.NET, not configured for System.Diagnostics tracing, how could I 'catch' the trace output?

Bonus question: Can I do something with Elmah to catch and log this info? Our ASP.NET app currently uses Elmah for error logging, but that's all I know on that side of things.

Was it helpful?

Solution

write code for attaching a trace file listener programtically and enable tracing in web.config - you will be done after that

OTHER TIPS

I think that, as long as the library outputs trace information, this information could be captured with any specialized tool (like DebugView) or even a house grown tool, regardless of ASP.NET configuration.

I see this is old and answered, but.. I have just had the same issue and I came up with

Exception class for tracelistener

namespace TrueNorth.Elmah.TraceListener
{
    internal class TraceInformation : Exception
    {
        internal TraceInformation(string message) : base(message){}
    }

    internal class TraceError: Exception
    {
        internal TraceError(string message) : base(message) { }
    }

    internal class TraceWarning : Exception
    {
        internal TraceWarning(string message) : base(message) { }
    }

    internal class TraceWrite : Exception
    {
        internal TraceWrite(string message) : base(message) { }
    }
}

The listener

namespace TrueNorth.Elmah.TraceListener
{
    internal class ElmahListener : System.Diagnostics.TraceListener
    {
        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
        {
            TraceEvent(eventCache, source, eventType, id, string.Format(format, args));
        }
        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message) //
        {
            Exception exception;
            switch (eventType)
            {
                case TraceEventType.Information:
                    exception = new TraceInformation(message);
                    break;
                case TraceEventType.Error:
                    exception = new TraceError(message);
                    break;
                case TraceEventType.Warning:
                    exception = new TraceWarning(message);
                    break;
                default:
                    exception = new TraceWrite(message);
                    break;
            }
            if (HttpContext.Current.Session == null)
            {
                ErrorLog.GetDefault(null).Log(new Error(exception));
            }
            else
            {
                ErrorSignal.FromCurrentContext().Raise(exception );
            }
        }
        public override void TraceTransfer(TraceEventCache eventCache, string source, int id, string message, Guid relatedActivityId)
        {
            base.TraceTransfer(eventCache, source, id, message, relatedActivityId);
        }
        public override void Write(string message)
        {
        }
        public override void WriteLine(string message)
        {
        }
    }
}

And, the GO code ( you can use your web.config)

Tracer.Register();

blogged at http://truenorthit.co.uk/2015/04/17/trace-listener-for-elmah-asp-mvc-exception-logger/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top