ASP.NET 中的跟踪侦听器

发布于 2024-07-24 08:49:21 字数 303 浏览 3 评论 0原文

我有一个输出跟踪信息的库程序集,以及一个通过 app.config 添加跟踪侦听器的客户端 winforms 应用程序。 如果我要在 ASP.NET 中使用该库,但未配置 System.Diagnostics 跟踪,我如何“捕获”跟踪输出?

额外问题:我可以使用 Elmah 来捕获并记录此信息吗? 我们的 ASP.NET 应用程序当前使用 Elmah 进行错误日志记录,但这就是我在这方面所知道的全部内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的他你的她 2024-07-31 08:49:21

我认为,只要库输出跟踪信息,就可以使用任何专用工具捕获该信息(例如 DebugView),甚至是内部开发的工具,无论 ASP.NET 配置如何。

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.

三岁铭 2024-07-31 08:49:21

我看到这是旧的并且已回答,但是..我刚刚遇到了同样的问题,我想出了

Exception 类用于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) { }
    }
}

侦听器

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)
        {
        }
    }
}

并且,GO 代码(您可以使用您的 web.config)

Tracer.Register();

博客在 http://truenorthit.co.uk/2015 /04/17/trace-listener-for-elmah-asp-mvc-exception-logger/

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/

不喜欢何必死缠烂打 2024-07-31 08:49:21

编写用于以编程方式附加跟踪文件侦听器的代码,并在 web.config 中启用跟踪 - 之后您就完成了

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文