Azure .NET Core 3.0 worker-can';不要让日志记录与应用程序洞察或事件日志一起工作

Azure .NET Core 3.0 worker-can';不要让日志记录与应用程序洞察或事件日志一起工作,azure,logging,.net-core,windows-services,worker,Azure,Logging,.net Core,Windows Services,Worker,我需要一个.NET 3.0工作者服务来登录Azure Application Insights和EventLog。这些都不行(几乎不行) 这是我的CreateHostBuilder: public static IHostBuilder CreateHostBuilder(string[] args) { return Host.CreateDefaultBuilder(args) .ConfigureSer

我需要一个.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"
                    });
                });
        }

2) Application Insights仅当注释掉
.AddApplicationInsightsTelemetryWorkerService()
并且
日志记录.AddApplicationInsights(“8d3bc77d-1cc3-4c4a-83e4-6d8aaa87f8f7”)中硬编码检测键时才能获取记录。
。如何从应用程序设置中获取密钥

3) 为什么这么麻烦?

更新 以下是完整的
应用程序开发设置

{
  "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 更改了日志记录: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

因此,我更改了
ConfigureServices()
ConfigureLogging()
的顺序,并使用了更新2中的
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
        }
公共静态IHostBuilder CreateHostBuilder(字符串[]args)
{
返回Host.CreateDefaultBuilder(args)
.ConfigureLogging(日志=>
{
logging.ClearProviders();
logging.AddConsole();
logging.AddEventLog(新的事件日志设置
{
SourceName=“PNWatchdog”,
LogName=“看门狗”
});
})
.ConfigureServices((主机上下文,服务)=>
{
services.AddHostedService()
.AddApplicationInsightsTelemetryWorkerService();
})
.UseWindowsService();//仅限windows功能
}

建议使用AddApplicationInsightsTelemetryWorkerService()在WorkerService中启用应用程序洞察。它可以从appsettings.json获取ikey。这将在内部设置日志提供程序(applicationinsightsloggingprovider)

ApplicationInsights日志提供程序默认情况下仅捕获级别为“警告”或更高的日志()。这是我的第一个怀疑。你能记录一些警告或错误,看看是否被捕获。
或者将application insights捕获的默认级别更改为捕获信息或跟踪级别。()

直到出现类似“Application Insights Telemetry WorkerService”的服务:-)

我使用
AddApplicationInsightsTelemetryWorkerService
使其工作。但只有当您不调用
logging.ClearProviders()时
或在
ConfigureServices
之前调用
ConfigureLogging
,否则将清除添加的日志提供程序

这项工作:

公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(日志=>
{
logging.ClearProviders();
logging.AddConsole();
})
.ConfigureServices((主机上下文,服务)=>
{
services.AddHostedService();
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"
            }
        }
    }
}

1) 您可能会注意到,我确实注释掉了
AddApplicationInsightsTelemetryWorkerService()
,所以我尝试使用它。我知道。但它根本不起作用2)注意我在问题中发布的记录器设置。日志级别是信息。不管怎样,我都试过了。App Insights中没有任何记录的迹象。这就是我问这个问题的原因。@alvipeo应用程序洞察的日志级别未设置为代码中的信息。请仔细阅读此答案中的第二个链接:App insights需要它自己的部分
ApplicationInsights
我确实有这个部分。但正如我在评论中所写的那样,
AddApplicationInsightsTelemetryWorkerService()
会拾取它,但App Insights中没有显示任何内容(当
logger.LogAnything
时)。因此,进入AppInsights的唯一方法是使用
logging.AddApplicationInsights(“8d3bc77d-1cc3-4c4a-83e4-6d8aaa87f8f7”)
,它根本不会从
app.settings
中获取检测键。粘贴的appsettings不会从applicationinsights部分获取检测键。请发布完整的appsettings.json-它应该有applicationinsights:instrumentationkey和logging.applicationinsights来控制日志记录级别。并仅使用AddApplicationInsightsTelemetryWorkerService()。将此示例用作参考:您正在清除所有提供程序<代码>logging.ClearProviders()请模仿此处的完整工作示例:重新设置EventLog,如果设置了自定义SourceName或LogName,则需要至少以管理员身份运行一次才能成功创建它们。如果失败,日志记录将禁用未来的日志记录,并向默认的“应用程序/应用程序”事件日志写入一条消息,其中包含一条消息,表示日志记录失败。看到自定义日志名被指定很奇怪,通常应用程序会登录到“应用程序”日志。通常将SourceName设置为应用程序名称。此外,appSettings.json文件中EventLog的日志设置应嵌套在“Logging”对象下,而不是位于根目录下(请参阅)如何从应用程序设置中获取密钥?->你可以发布你的app.config,包括你如何设置插装键吗?我的意思是当使用
时。AddApplicationInsightsTelemetryWorkerService()
会获取插装键,但AppInsights中没有记录。因此,让他们留在那里的唯一方法是
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"
            }
        }
    }
}