简体   繁体   English

Log4net 作为跟踪记录到 AppInsight

[英]Log4net Logging to AppInsight as Trace

Currently I am using Log4net Appender for App Insight, to log the error to Application Insight.目前我正在使用 Log4net Appender for App Insight,将错误记录到 Application Insight。

 TelemetryConfiguration.Active.InstrumentationKey = "XXXX";

            var tc = new TelemetryClient();
            var log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            XmlConfigurator.Configure();
            log.Error(ex+ "@"+ CodeBlock); **IN this case its loggin as Trace.**             log.Fatal(ex + "@" + CodeBlock);
            log.Info(ex + "@" + CodeBlock);

            TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true;
            tc.Flush();
            Thread.Sleep(5000);

In this case, all of the events are logged as Trace.在这种情况下,所有事件都记录为 Trace。 But where I expect it logged as exception.但是我希望它记录为异常的地方。 I can use telemetry object to pass the exception but then there won't be any result of using of Log4net.我可以使用遥测对象来传递异常,但是使用 Log4net 不会有任何结果。 We can't pass the log entry to any other logging tools except App insight.我们无法将日志条目传递给除 App Insight 之外的任何其他日志记录工具。

For logged as exception, you should use this line of code log.Error("your message",new Exception());对于记录为异常,您应该使用这行代码log.Error("your message",new Exception());

In visual studio output window, if you use log.Error("your message",new Exception());在 Visual Studio 输出窗口中,如果使用log.Error("your message",new Exception()); , you can see the Application Insights Telemetry has a name ends with Exception , like below: ,您可以看到Application Insights Telemetry的名称以Exception结尾,如下所示: 在此处输入图片说明

Then go to azure portal, you can see the it goes as Exception, screenshot below:然后转到azure portal,你可以看到它作为异常,截图如下: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM