无法将 log4net 日志发送到应用程序洞察

时间:2021-01-04 13:31:04

标签: docker logging .net-core log4net azure-application-insights

我有一个在 Kubernetes 中运行的 dotnetcore 服务。对于日志记录,我使用了 log4net。我正在尝试将服务日志发送到 application-insights,但日志没有显示在那里。

我在我的服务中添加了以下配置以获取应用程序洞察。

运行时版本:netcoreapp3.1 version-2.31.0.1

托管环境: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";
    }
}

我还为服务添加了 APPINSIGHTS_INSTRUMENTATIONKEY 环境变量。我能看到服务的请求信息,但不能看到日志。 application-insights 的日志部分只显示 2 个日志,但它不是来自应用程序 (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.

以下是我浏览过的一些链接。

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/

但是,日志仍然没有添加到应用程序洞察中。有什么我在这里想念的吗?是否可以像在 nodejs 应用程序中一样将 Console.WriteLine 日志添加到 application-insights 中,我们可以通过启用 application-insights 模块的配置来发送它?你能帮我吗?

谢谢...

1 个答案:

答案 0 :(得分:0)

感谢 Peter Drier 的回答,我认为他提供的以下代码对您有用。

更多详情,可以参考原帖。

Log4Net and Application Insights-no data is coming through

<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>