[英]Log4Net and Application Insights-no data is coming through
我在我的 C# 控制台應用程序中使用 Log4Net,並且希望記錄的事件也出現在 Application Insights 中。
我添加了 Application Insights 以及 Application Insights Log4Net Appender。
我已按照此處的說明進行操作: https://jan-v.nl/post/using-application-insights-in-your-log4net-application沒有運氣。
Log4Net 記錄良好。 但是,當我將 go 轉到 Application Insights 儀表板時,我看到:“該應用程序在應用程序洞察力中沒有數據。”
在我的主 cs 文件的開頭,我有這個:
var telemetryClient = new TelemetryClient { InstrumentationKey = ConfigurationManager.AppSettings["applicationInsights"] };
最后,有這樣的:
telemetryClient.Flush();
在我的 app.config 文件中,我有這個:
<log4net>
<root>
<level value="INFO" />
<appender-ref ref="FileAppender" />
<appender-ref ref="aiAppender" />
</root>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C:\logs\logfile.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
</layout>
</appender>
<appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
</appender>
</log4net>
當我運行應用程序時,我在 output window 中看到了這種東西:
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Message","time":"2017-07-04T10:26:15.7741453Z","tags":{"ai.internal.sdkVersion":"log4net:2.2.0-220","ai.cloud.roleInstance":"xxx","ai.user.id":"AzureAD\\xxx"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"xxx","severityLevel":"Information","properties":{xxx"}}}}
我錯過了什么?
Log4Net AI appender 不會使用您創建的 TelemtryClient。
像這樣設置 AI Instrumentation Key:
TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.AppSettings["applicationInsights"];
請參閱https://github.com/Microsoft/ApplicationInsights-dotnet-logging
自從提出這個問題以來,ApplicationInsights 已經發展。 由於各種原因,原來的靜態 TelemetryConfiguration.Active 現在已經過時了。 那些仍然試圖快速設置他們的 log4net 配置的人可以這樣做(在他們的 log4net 配置文件中):
<appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
<threshold value="INFO" />
<InstrumentationKey value="12345678-7946-1234-1234-8b330fbe1234" />
</appender>
適用於dotnetcore 2.2和Microsoft.Extensions.Logging.ApplicationInsights 2.10
我還有一個問題,我的日志沒有發送到 AI。 找了很久之后。 在我看到在調試時我對 AI 的調用和遙測通道被執行后,我走上了這條路,但是當我運行 api 時,沒有發送任何數據。
我發現它與使用的遙測通道有關(查看微軟的在線文檔什么是遙測通道,但它充當您的應用程序和 azure 應用程序洞察力之間的中間件)。
如果您在 DEV 機器上工作(沒有服務器!),您可以將此標志設置為 true
在 StartUp.ConfigureServices 中:
#if DEBUG
TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true;
#endif
之后,我的所有日志記錄都發送到 AI,可以在跟蹤表中找到,並且可以查看每個請求。
我在他們的 GIT 頁面上找到了答案: https : //github.com/Microsoft/ApplicationInsights-dotnet/issues/964
另一個注意:默認級別是警告,要將其設置為較低級別,請將其添加到 program.cs
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.ConfigureLogging(logging =>
{
// Optional: Apply filters to configure LogLevel Trace or above is sent to
// ApplicationInsights for all categories.
logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
// Additional filtering For category starting in "Microsoft",
// only Warning or above will be sent to Application Insights.
logging.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Warning);
})
.UseStartup<Startup>();
}
如果您不想在 log4net.config 中注入檢測密鑰,可以在 AddApplicationInsightsTelemetry 方法中傳遞它:
services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
{
ConnectionString = Configuration["ApplicationInsights:ConnectionString"],
EnableActiveTelemetryConfigurationSetup = true
});
(現在使用連接字符串優於遙測密鑰。)
只要您擁有 log4netappender package,它就應該可以工作
並且應將應用洞察配置為附加程序
<appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline"/>
</layout>
</appender>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.