Not able to send log4net logs to application insights

I have dotnetcore service which is running in Kubernetes. For logging, I have used the log4net. I am trying to send the logs of service to application-insights but the logs are not showing there.

I have added the below configuration in my service for application insights.

Runtime version: netcoreapp3.1 version-2.31.0.1

Hosting environment: Azure-cluster

app-service.csproj

<ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.16.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.Log4NetAppender" Version="2.16.0" />
    ...
<ItemGroup>

log4net.config

   ...
<appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
  <InstrumentationKey name="AppInsightsKey" value="abcdefgh-abcd-abcd-abcd-abcdefghijkl" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%message%newline"/>
  </layout>
</appender>

startup.cs

 public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            Logger.ConfigureLog4Net("./logs/app.log", Configuration)
            ...
        }
 public void ConfigureServices(IServiceCollection services)
        {
            // The following line enables Application Insights telemetry collection.
            services.AddApplicationInsightsTelemetry();
            services.AddSingleton<ITelemetryInitializer, CloudRoleNameTelemetryInitializer>();
            // This code adds other services for the application.
            services.AddMvc();
            ...
        }

CloudRoleNameTelemetryInitializer.cs

using System.Net;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
public class CloudRoleNameTelemetryInitializer : ITelemetryInitializer
{
   public void Initialize(ITelemetry telemetry)
    {
      // set custom role name here
      telemetry.Context.Cloud.RoleName = "app-service";
    }
}

I have also added the APPINSIGHTS_INSTRUMENTATIONKEY environment variable for service. I am able to see the request information for service but not logs. Only 2 logs are showing in the log section of application-insights but it is not from the application(log4net).

No XML encryptor configured. Key {XXX-XXX-XXX-XXX-XXX} may be persisted to storage in unencrypted form.
Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when the container is destroyed.

Below are some links I have explored.

https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-trace-logs

https://medium.com/@muralimohan.mothupally/application-insights-integration-with-log4net-e1bef68fe3c8

https://blog.ehn.nu/2014/11/using-log4net-for-application-insights/

https://jan-v.nl/post/using-application-insights-in-your-log4net-application/

But still, logs are not adding in the application insights. Is there anything I am missing here? Also is it possible to add Console.WriteLine logs to application-insights as like in nodejs app we can send it by enabling the application-insights module’s configuration? Can you please help me?

Thanks…

Source: Docker Questions

LEAVE A COMMENT