gpt4 book ai didi

c# - 将 Application Insights 与 ILoggerFactory 结合使用

转载 作者:可可西里 更新时间:2023-11-01 09:00:03 27 4
gpt4 key购买 nike

我正在尝试将异常记录到 Application Insights。我通过直接调用 TelemetryClient.TrackException 成功地做到了这一点。但是,我想在我的代码中对此进行抽象,以防将来我想登录到其他平台,因此我只想坚持使用 ILogger 接口(interface)。

我发现您可以使用 ILoggerFactory.AddApplicationInsights(已实现 here),但无论我做了什么,我都没有看到日志显示在 ApplicationInsights 中。

下面是我的代码:

Startup.cs

    IConfigurationRoot Configuration { get; set; }
ILoggerFactory LoggerFactory { get; set; }
IServiceProvider ServiceProvider { get; set; }

public Startup( IHostingEnvironment hostingEnvironment, ILoggerFactory loggerFactory )
{
this.LoggerFactory = loggerFactory;
string configurationFilePath = "abc.json";

this.Configuration = new ConfigurationBuilder()
.SetBasePath( hostingEnvironment.ContentRootPath )
.AddJsonFile( configurationFilePath, optional: true, reloadOnChange: true )
.AddEnvironmentVariables()
.Build();
}

public void Configure(
IApplicationBuilder applicationBuilder,
IHostingEnvironment hostingEnvironment,
ILoggerFactory loggerFactory,
IServiceProvider serviceProvider )
{
this.ServiceProvider = serviceProvider;
loggerFactory.AddApplicationInsights( serviceProvider );
applicationBuilder.UseMvc();
}

public void ConfigureServices( IServiceCollection services )
{
services.AddApplicationInsightsTelemetry( this.Configuration );
services.AddMvc( .... // A bunch of options here ... )
}

然后,我尝试像这样在我的 Controller 中使用它:

    ILogger<XController> Logger { get; set; }

public XController( ILogger<XController> logger )
{
this.Logger = logger;
}

[HttpPost]
[Route( "v3.0/abcd" )]
public async Task PostEvent( [FromBody] XEvent xEvent )
{
this.Logger.LogError( 0, new Exception( "1234" ), "1234" );
}

但是,我根本没有看到与请求相关的任何异常。如果我用 TelemetryClient.TrackException 替换 Logger.LogError 行(并首先创建 TelemetryClient),那么我可以毫无问题地看到异常.

我不知道我做错了什么。有人能帮忙吗?

最佳答案

根据您的描述,我建议您尝试使用以下代码启用 ILogger 将错误记录到 ApplicationInsights。

您可以直接使用 loggerFactory.AddApplicationInsights() 方法来启用 ApplicationInsights ILogger。

更多细节,你可以引用下面的代码:

启动类:

public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
// Add framework services.
services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{

loggerFactory.AddApplicationInsights(app.ApplicationServices,LogLevel.Warning);

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}

appsettings.json:

{

"ApplicationInsights": {
"InstrumentationKey": "yourkey"
}
}

结果:

enter image description here

enter image description here


更新:

在搜索功能中找到的记录。

enter image description here

关于c# - 将 Application Insights 与 ILoggerFactory 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45022693/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com