cro*_*oxy 7 c# log4net azure-application-insights
我们当前的项目由一个 ASP.NET 应用程序以及一些较小的控制台应用程序组成。我们使用 Microsoft 的Application Insights进行 ASP.NET 应用程序的应用程序管理。
现在,我们希望将Application Insights集成到控制台应用程序中,以便能够将日志记录集中到 Azure。
我们的遗留日志记录是通过Log4Net实现的。在控制台应用程序中配置Application Insights Core及其相应的 Log4Net Appender 后,我们能够按预期在 Azure 中查看日志条目。来自控制台应用程序的每条日志消息都会发送到 Azure,但我们附加附加对象的日志除外。例如,我们的日志消息的严重性级别为Error,其中包含异常对象作为第二个参数:
Log.Error("This looks like an error", ex);
Run Code Online (Sandbox Code Playgroud)
这些日志条目根本不会显示在 Azure 中。仅当将异常对象放入消息中时才会显示它们,例如:
Log.Error($"This looks like an error. Exception: {ex}");
Run Code Online (Sandbox Code Playgroud)
因此,Log4Net 中的异常对象似乎有大小限制,但实际消息却没有?如果是的话,这可以以任何方式配置吗?因为改变每个'Log.Error();' 在整个项目中不是一个选择。
我们的ApplicationInsights.config文件:
<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<InstrumentationKey>[Our key]</InstrumentationKey>
<TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel"/>
<TelemetryProcessors>
<Add Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor, Microsoft.AI.ServerTelemetryChannel">
<MaxTelemetryItemsPerSecond>5</MaxTelemetryItemsPerSecond>
<ExcludedTypes>Trace;Exception</ExcludedTypes>
</Add>
</TelemetryProcessors>
</ApplicationInsights>
Run Code Online (Sandbox Code Playgroud)
编辑:
Log4Net配置:
<!-- ... -->
<!-- Configuration of logfile and console appender -->
<!-- ... -->
<root>
<level value="ALL" />
<appender-ref ref="logfile" />
<appender-ref ref="console" />
<appender-ref ref="aiAppender" />
</root>
<appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
</appender>
Run Code Online (Sandbox Code Playgroud)
其他附加程序没有问题。
我想到了。导致问题的原因是,只要将异常对象附加到 Log4Net 的任何日志记录方法中:
Log.Error(message, exception);
Log.Warn(message, exception);
Log.Info(message, exception);
Run Code Online (Sandbox Code Playgroud)
特定日志条目不作为跟踪处理,而是作为Application Insights中的异常处理。因此,调整 Azure 中的Application Insights选项卡中的筛选器以显示异常就足够了。现在我也 可以看到Azure 中的条目。Log.Error(message, exception)
我不知道为什么要这样处理,因为这使得第三方软件处理日志变得更加复杂(例如: Exceptions在Application Insights等中没有明确的消息字段)。
| 归档时间: |
|
| 查看次数: |
3633 次 |
| 最近记录: |