如何使用 Autofac 在框架控制台应用程序中添加 ApplicationInsights 日志记录提供程序
How to add ApplicationInsights Logging Provider in Framework Console App Using Autofac
我正在开发一个 .NET(完整框架 4.7.1)控制台应用程序,该应用程序将 AutoFac 用于 DI 目的。
我们正在缓慢迁移到 .NET Core,并已切换到使用 Microsoft.Extensions.Logging.Abstractions 提供的 ILogger 抽象。
我已经使用以下方法在 AutoFac 中连接了 ILogger<> 和 ILoggerFactory
private static void RegisterLogging(ContainerBuilder builder)
{
builder.RegisterType<LoggerFactory>().As<ILoggerFactory>().SingleInstance();
builder.RegisterGeneric(typeof(Logger<>)).As(typeof(ILogger<>)).InstancePerDependency();
}
这取决于 Microsoft.Extensions.Logging - 它似乎有效。
现在我想连接 Application Insights Logging 提供程序,但是我能找到的所有文档都只提到了如何将它添加到 .NET Core DI 容器,并查看了各种 repos 上的源代码,我是对如何去做有点迷惑。
我想我也许可以这样做:
builder.RegisterType<ApplicationInsightsLoggerProvider>().As<ILoggerProvider>();
但这取决于 IOptions<TelemetryConfiguration> telemetryConfigurationOptions
和 IOptions<ApplicationInsightsLoggerOptions> applicationInsightsLoggerOptions
我都没有。
有没有人做过这个,或者对如何完成有建议?
我这样做成功了:
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging();
serviceCollection.AddApplicationInsightsTelemetryWorkerService();
builder.Populate(serviceCollection);
不是最好的解决方案,但我想它确实允许我使用 ServiceCollection 扩展方法的步法,所以如果 nobdoy 有更好的答案,我可能不得不接受它
我正在开发一个 .NET(完整框架 4.7.1)控制台应用程序,该应用程序将 AutoFac 用于 DI 目的。
我们正在缓慢迁移到 .NET Core,并已切换到使用 Microsoft.Extensions.Logging.Abstractions 提供的 ILogger 抽象。
我已经使用以下方法在 AutoFac 中连接了 ILogger<> 和 ILoggerFactory
private static void RegisterLogging(ContainerBuilder builder)
{
builder.RegisterType<LoggerFactory>().As<ILoggerFactory>().SingleInstance();
builder.RegisterGeneric(typeof(Logger<>)).As(typeof(ILogger<>)).InstancePerDependency();
}
这取决于 Microsoft.Extensions.Logging - 它似乎有效。
现在我想连接 Application Insights Logging 提供程序,但是我能找到的所有文档都只提到了如何将它添加到 .NET Core DI 容器,并查看了各种 repos 上的源代码,我是对如何去做有点迷惑。
我想我也许可以这样做:
builder.RegisterType<ApplicationInsightsLoggerProvider>().As<ILoggerProvider>();
但这取决于 IOptions<TelemetryConfiguration> telemetryConfigurationOptions
和 IOptions<ApplicationInsightsLoggerOptions> applicationInsightsLoggerOptions
我都没有。
有没有人做过这个,或者对如何完成有建议?
我这样做成功了:
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging();
serviceCollection.AddApplicationInsightsTelemetryWorkerService();
builder.Populate(serviceCollection);
不是最好的解决方案,但我想它确实允许我使用 ServiceCollection 扩展方法的步法,所以如果 nobdoy 有更好的答案,我可能不得不接受它