【问题标题】:Not able to send log4net logs to application insights无法将 log4net 日志发送到应用程序洞察
【发布时间】:2021-01-05 10:06:05
【问题描述】:

我有在 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/

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

谢谢...

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2018-04-05
      • 1970-01-01
      • 2019-03-04
      • 2022-01-13
      • 2021-11-12
      • 2017-11-10
      • 2022-08-23
      • 1970-01-01
      • 2018-08-15
      相关资源
      最近更新 更多