代码之家  ›  专栏  ›  技术社区  ›  AlexB

.NET Core 3.0 worker-无法使日志记录与应用程序洞察或事件日志一起工作

  •  0
  • AlexB  · 技术社区  · 5 年前

    我需要一个.NET3.0工作服务来登录到AzureApplicationInsights和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"
                        });
                    });
            }
    
    

    (一) 不管我做什么

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

    2个) 应用洞察 .AddApplicationInsightsTelemetryWorkerService() 已被注释掉 仪表键在 logging.AddApplicationInsights("8d3bc77d-1cc3-4c4a-83e4-6d8aaa87f8f7") . 该怎么做才能从应用程序设置中获取密钥?

    为什么这么麻烦?

    更新

    这是全部 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

    应用程序权限 登录中

    {
      "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"
      }
    }
    

    分辨率

    多亏了彼得·邦斯!

    ConfigureServices() ConfigureLogging() 使用 appsettings 从更新2开始,现在开始工作了!所以我们开始了:

    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
            }
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   Peter Bons    5 年前

    直到出现类似于“应用程序洞察遥测工作人员服务”的内容:

    我用它工作 AddApplicationInsightsTelemetryWorkerService . 但前提是你要么不打电话 logging.ClearProviders(); ConfigureLogging 之前 ConfigureServices 否则将清除添加的日志提供程序。

    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"
                }
            }
        }
    }
    
        2
  •  0
  •   cijothomas    5 年前

    AddApplicationInsightsTelemetryWorkerService()是在WorkerService中启用应用程序洞察的推荐方法。它可以从appsettings.json获取ikey。这将在内部设置日志提供程序(applicationinglightsloggingprovider)。

    https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service#ilogger-logs ). 所以这是我的第一个怀疑。你能记录一些警告或错误,看看是否被捕获。 https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#control-logging-level )

    推荐文章