Reputation: 3580
I'm using Log4Net in my my C# console application and want the logged events to appear in Application Insights as well.
I've added Application Insights as well as the Application Insights Log4Net Appender.
I've followed the instructions here: https://jan-v.nl/post/using-application-insights-in-your-log4net-application with no luck.
Log4Net is logging fine. However, when I go to the Application Insights dashboard, I see this: "THE APPLICATION HAS NO DATA IN APPLICATION INSIGHTS."
At the beginning of my main cs file, I've got this:
var telemetryClient = new TelemetryClient { InstrumentationKey = ConfigurationManager.AppSettings["applicationInsights"] };
at the end, there is this:
telemetryClient.Flush();
In my app.config file, I have this:
<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>
When I run the application, I see this sort of thing in the 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"}}}}
What am I missing?
Upvotes: 3
Views: 5475
Reputation: 3453
If you don't want to inject your instrumentation key in your log4net.config you can pass it in the AddApplicationInsightsTelemetry method:
services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
{
ConnectionString = Configuration["ApplicationInsights:ConnectionString"],
EnableActiveTelemetryConfigurationSetup = true
});
(Using the connection string is prefered over the telemetry key now. )
It should work as long as you have the log4netappender package
And app insights should be configured as an appender
<appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline"/>
</layout>
</appender>
Upvotes: 0
Reputation: 1397
ApplicationInsights has evolved since this question was asked. The original static TelemetryConfiguration.Active is now obsolete for various reasons. Those still trying to get their log4net config up quickly can do this (in their log4net configuration file):
<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>
Upvotes: 5
Reputation: 2644
Applies to dotnetcore 2.2 and Microsoft.Extensions.Logging.ApplicationInsights 2.10
I also had an issue that my loggings were not send to AI. After looking for a long time. I came on this path after i had seen that in debugging my calls to AI and the telemetry channel were executed, but when just running the api, no data was sent.
I found out it had something to do with the Telemetry Channel that is used (look in the online documention of microsoft what the telemetry channel, but it acts as a middleware between your application and azure application insight).
If you are working on a DEV machine (no SERVERS!), you can set this flag to true
In StartUp.ConfigureServices:
#if DEBUG
TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true;
#endif
And after that all my logging were sent to AI, and could be found in traces table, and can be seen for each request.
I found the answer on their GIT page: https://github.com/Microsoft/ApplicationInsights-dotnet/issues/964
Another note: the default level is Warning, to set this to a lower level, add this to 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>();
}
Upvotes: 2
Reputation: 119
Log4Net AI appender will not use the TelemtryClient you create.
Set AI Instrumentation Key like this:
TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.AppSettings["applicationInsights"];
See, https://github.com/Microsoft/ApplicationInsights-dotnet-logging
Upvotes: 2