如何将 Log4Net 日志发送到 Application Insights for .NET Core 3.1 Web 应用程序?
Posted
技术标签:
【中文标题】如何将 Log4Net 日志发送到 Application Insights for .NET Core 3.1 Web 应用程序?【英文标题】:How to send Log4Net logs to Application Insights for .NET Core 3.1 web application? 【发布时间】:2021-11-12 10:42:14 【问题描述】:无法将 Log4Net 日志刷新到用于 net core 3.1 的 Web 应用中的 Azure 应用洞察
这是项目文件。
log4net.config
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<!--Application Insights Appender-->
<appender name='aiAppender'
type='Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender,
Microsoft.ApplicationInsights.Log4NetAppender'>
<layout type='log4net.Layout.PatternLayout'>
<conversionPattern value='%message%newline' />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="aiAppender" />
</root>
</log4net>
ApplicationInsights.config
<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<InstrumentationKey>..Added InstrumentationKey here..</InstrumentationKey>
</ApplicationInsights>
Startup.cs
public void ConfigureServices(IServiceCollection services)
services.AddApplicationInsightsTelemetry();
Nuget 包:
appsettings.json
"ApplicationInsights":
"InstrumentationKey": "<Added instrumentation key here>"
,
"Logging":
"ApplicationInsights":
"LogLevel":
"Default": "Error"
【问题讨论】:
【参考方案1】:我终于通过更新 Startup.cs 中的 ConfigureServices() 来完成这项工作
public void ConfigureServices(IServiceCollection services)
services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
InstrumentationKey = Configuration["ApplicationInsights:InstrumentationKey"],
EnableActiveTelemetryConfigurationSetup = true
);
【讨论】:
以上是关于如何将 Log4Net 日志发送到 Application Insights for .NET Core 3.1 Web 应用程序?的主要内容,如果未能解决你的问题,请参考以下文章
如何以编程方式强制 log4net 释放日志文件以便可以访问它?