IHostedService 控制台应用程序中的 Application Insights
Application Insights in IHostedService console application
我正在尝试使用 IHostedService
在控制台应用程序中启用 Application Insights(目前,它是一个简单的控制台应用程序,我们 运行 作为 WebJob,将来在容器中)。
据我所知,在以下代码中,到目前为止我们没有任何扩展来全局注册 Application Insights 作为 ILogger
的实现:
public static class Program
{
public static Task Main(string[] args)
{
var hostBuilder = new HostBuilder()
.ConfigureHostConfiguration(config =>
{
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddJsonFile("appsettings.json", optional: false);
config.AddEnvironmentVariables();
})
.ConfigureLogging((context, logging) =>
{
logging.AddConfiguration(context.Configuration.GetSection("Logging"));
if (context.HostingEnvironment.IsDevelopment())
{
logging.AddConsole();
}
else
{
//TODO: register ApplicationInsights
}
});
return hostBuilder.RunConsoleAsync();
}
}
到目前为止,我发现我应该能够使用记录器的自定义实现来设置所有内容,即 public class ApplicationInsightsLogger : ILogger
,然后...将其注册到容器中以便 DI 解析它。
这个方向对吗?
我做了一个可以从 IHost
或 IWebHost
中使用的扩展:
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.ApplicationInsights;
public static class LoggingBuilderExtensions
{
public static ILoggingBuilder AddLogging(this ILoggingBuilder loggingBuilder)
{
loggingBuilder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
loggingBuilder.AddAzureWebAppDiagnostics();
loggingBuilder.AddApplicationInsights();
return loggingBuilder;
}
}
因为我没有在上下文中发送(HostBuilderContext
或 WebHostBuilderContext
),所以我可以像这样在任一应用程序类型中使用它:
new HostBuilder().ConfigureLogging(loggingBuilder => loggingBuilder.AddLogging())
或
WebHost.CreateDefaultBuilder().ConfigureLogging(loggingBuilder => loggingBuilder.AddLogging())
如果您需要上下文中的特定 属性(例如环境类型),您可以提取它并将其作为参数发送到扩展。
这里有一个参考:https://github.com/Microsoft/ApplicationInsights-dotnet-logging/blob/develop/src/ILogger/Readme.md
我正在尝试使用 IHostedService
在控制台应用程序中启用 Application Insights(目前,它是一个简单的控制台应用程序,我们 运行 作为 WebJob,将来在容器中)。
据我所知,在以下代码中,到目前为止我们没有任何扩展来全局注册 Application Insights 作为 ILogger
的实现:
public static class Program
{
public static Task Main(string[] args)
{
var hostBuilder = new HostBuilder()
.ConfigureHostConfiguration(config =>
{
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddJsonFile("appsettings.json", optional: false);
config.AddEnvironmentVariables();
})
.ConfigureLogging((context, logging) =>
{
logging.AddConfiguration(context.Configuration.GetSection("Logging"));
if (context.HostingEnvironment.IsDevelopment())
{
logging.AddConsole();
}
else
{
//TODO: register ApplicationInsights
}
});
return hostBuilder.RunConsoleAsync();
}
}
到目前为止,我发现我应该能够使用记录器的自定义实现来设置所有内容,即 public class ApplicationInsightsLogger : ILogger
,然后...将其注册到容器中以便 DI 解析它。
这个方向对吗?
我做了一个可以从 IHost
或 IWebHost
中使用的扩展:
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.ApplicationInsights;
public static class LoggingBuilderExtensions
{
public static ILoggingBuilder AddLogging(this ILoggingBuilder loggingBuilder)
{
loggingBuilder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
loggingBuilder.AddAzureWebAppDiagnostics();
loggingBuilder.AddApplicationInsights();
return loggingBuilder;
}
}
因为我没有在上下文中发送(HostBuilderContext
或 WebHostBuilderContext
),所以我可以像这样在任一应用程序类型中使用它:
new HostBuilder().ConfigureLogging(loggingBuilder => loggingBuilder.AddLogging())
或
WebHost.CreateDefaultBuilder().ConfigureLogging(loggingBuilder => loggingBuilder.AddLogging())
如果您需要上下文中的特定 属性(例如环境类型),您可以提取它并将其作为参数发送到扩展。
这里有一个参考:https://github.com/Microsoft/ApplicationInsights-dotnet-logging/blob/develop/src/ILogger/Readme.md