首页 > 解决方案 > .NET Core 3.0 工作者 - 无法使用 Application Insights 或 EventLog 进行日志记录

问题描述

我需要一个 .NET 3.0 辅助服务来登录 Azure Application Insights 和 EventLog。这些都不起作用(几乎)!

这是我的CreateHostBuilder

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    IConfiguration configuration = hostContext.Configuration;

                    WatchdogOptions options = configuration.GetSection("WorkerOptions").Get<WatchdogOptions>();
                    if (options == null) throw new WatchdogException("WorkerOptions settings are not set");

                    services.AddSingleton(options);

                    services.AddHostedService<Worker>()
                    // .AddApplicationInsightsTelemetryWorkerService();
                    ;
                })
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    //logging.AddConsole();
                    logging.AddApplicationInsights("<instr-key>");
                    logging.AddEventLog(new EventLogSettings
                    {
                        SourceName = "PNWatchdog",
                        LogName = "Watchdog"
                    });
                });
        }

1)无论我做什么,EventLog 都没有来自我的工人的任何记录。我确实在应用程序设置中设置了日志记录级别:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "EventLog": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

2) Application Insights仅在.AddApplicationInsightsTelemetryWorkerService()被注释掉并且检测密钥被硬编码在logging.AddApplicationInsights("8d3bc77d-1cc3-4c4a-83e4-6d8aaa87f8f7"). 应该怎么做才能从应用程序设置中获取密钥?

3)为什么这么麻烦?

更新

这是完整的app.development.settings

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
    "EventLog": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    }
  },
  "ApplicationInsights": {
    "InstrumentationKey": "8d3bc77d-1cc5-what-ever-0000000000"
  }
}

更新 2

ApplicationInsights添加到日志记录部分:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
    "EventLog": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    } 
  },
  "ApplicationInsights": {
    "InstrumentationKey": "8d3bc77d-1cc5-4c4a-83e4-6d8aaa87f8f7"
  }
}

更新 3

属性名称更改为Logging:ApplicationInsights:LogLevel

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
    "EventLog": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    },
    "ApplicationInsights": {
      "LogLevel": {
        "PushNotificationsWatchdog.Worker": "Information"
      }
    }
  },
  "ApplicationInsights": {
    "InstrumentationKey": "8d3bc77d-1cc5-4c4a-83e4-6d8aaa87f8f7"
  }
}

同样的事情 - App Insights 中没有记录。检测密钥正确。

解析度

感谢@peter-bons!

所以我改变了UPDATE 2中的ConfigureServices()andConfigureLogging()和使用的顺序,它现在可以工作了!appsettings所以我们开始了:

public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.AddConsole();
                    logging.AddEventLog(new EventLogSettings
                    {
                        SourceName = "PNWatchdog",
                        LogName = "Watchdog"
                    });
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>()
                        .AddApplicationInsightsTelemetryWorkerService();
                })
                .UseWindowsService(); // windows only feature
        }

标签: azurelogging.net-corewindows-servicesworker

解决方案


直到有类似“Application Insights Telemetry WorkerService”的东西:-)

我让它使用AddApplicationInsightsTelemetryWorkerService. 但前提是您要么不调用,要么不logging.ClearProviders();调用ConfigureLoggingConfigureServices否则添加的日志记录提供程序将被清除。

这有效:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.AddConsole();
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();
                    services.AddApplicationInsightsTelemetryWorkerService();
                });

使用此配置:

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  },
  "ApplicationInsights": {
      "InstrumentationKey": "xxx"
    }
  }

正如您在输出中看到的那样,AI 密钥被正确拾取:

Application Insights Telemetry: {
    "name": "Microsoft.ApplicationInsights.Dev.3b40adb096064da0816e7b8579aa443c.Message",
    "time": "2019-11-13T07:52:11.0027057Z",
    "iKey": "xxx",
    "tags": {
        "ai.application.ver": "1.0.0.0",
        "ai.cloud.roleInstance": "xxx",
        "ai.internal.sdkVersion": "il:2.11.0-21511",
        "ai.internal.nodeName": "xxx"
    },
    "data": {
        "baseType": "MessageData",
        "baseData": {
            "ver": 2,
            "message": "Application started. Press Ctrl+C to shut down.",
            "severityLevel": "Information",
            "properties": {
                "{OriginalFormat}": "Application started. Press Ctrl+C to shut down.",
                "CategoryName": "Microsoft.Hosting.Lifetime",
                "DeveloperMode": "true"
            }
        }
    }
}

推荐阅读