gpt4 book ai didi

azure - NLog with Application Insights - 将异常记录为异常而不是跟踪

转载 作者:行者123 更新时间:2023-12-02 22:27:05 25 4
gpt4 key购买 nike

目前我正在使用使用服务堆栈构建的 .NET Core Web 应用程序。目前使用 NLog 提供日志记录,以 Azure Application Insights 作为目标。

当前,当我记录消息和异常时,我遇到了一些奇怪的行为:

Log.Fatal("test not implemented exception", new NotImplementedException()); //this logs under Exceptions in Application Insights
Log.Fatal(new NotImplementedException("test not implemented exception")); //this logs under Trace

我想要的是能够将第二行记录为异常而不是跟踪。有什么办法可以实现这个目标吗?

我无法找到此问题的答案,因此我将其作为问题发布。

NLog exceptions as Trace in Application Insights - 这篇没有提到如何将类型从 Trace 更改为 Exception

Application Insights - Logging exceptions - 这个没有提到NLog

https://github.com/Microsoft/ApplicationInsights-dotnet-logging/issues/102 - 这是最接近我需要的,但没有更新

编辑:因为注释标记无法正确显示图像和格式,所以我一直在使用以下代码行进行测试:

Log.Fatal("test no message - fatal", new NotImplementedException());
Log.Error(new NotImplementedException("test no message - error"));

结果如下:

enter image description here

编辑2:软件包版本如下:

NLog (4.6.8)
NLog.Web.AspNetCore (4.9.0)
Microsoft.ApplicationInsights.AspNetCore (2.8.2)
Microsoft.ApplicationInsights.NLogTarget (2.11.0)

编辑3:更多代码:

我们的服务接口(interface)层使用此代码。它实现了由服务堆栈提供的服务类的内容。通过如上所述定义 LogFactory,我们可以将其传递到我们的服务层。

此服务接口(interface)托管在同一解决方案下的单独项目中(我们将其称为 Api.ServiceInterface)。

AppServiceBase.cs

public abstract class AppServiceBase : Service
{
protected ILog Log { get; set; }
protected ICacheClient CacheClient { get; set; }

protected AppServiceBase()
{
Log = LogManager.GetLogger(GetType());
}

public AppServiceBase(ICacheClient cacheClient) : this()
{
CacheClient = cacheClient;
}

public UserSession UserSession => this.GetSession() as UserSession;
}

HelloService.cs(扩展自AppServiceBase的服务,因此可以直接调用上面AppServiceBase中的记录器)。

public class HelloService : AppServiceBase
{
public object Any(Hello request)
{
Log.Fatal("test no message - fatal 1", new NotImplementedException());
Log.Error(new NotImplementedException("test no message - error 1"));
return new HelloResponse { Result = $"Hola, {request.Name}!" };
}
}

我们使用以下 URL 调用此函数:

http://localhost/hello/name

它所做的只是返回一条文本,上面写着“Hola,name!”

不幸的是,我无法附加调试器 - 无论出于何种原因,我都无法让它到达断点。

我不知道我是否也需要在 Api.ServiceInterface 项目上拥有相同的库。请注意,Api.ServiceInterface 是一个类库类型的项目,在 .NET Standard 2.0 上运行。

最佳答案

注意 ServiceStack 5.8 已发布,修复了该问题。

查看了服务堆栈版本。 5.7 记录仪接口(interface)。这是行不通的:

// Will pass exception a string.Format parameter
Log.Fatal("test no message - fatal", new NotImplementedException());
// Will make Service Stack call Exception.ToString
Log.Error(new NotImplementedException("test no message - error"));

并且您需要采取此解决方法才能正确到达 NLog:

Log.Fatal(new NotImplementedException(), "test no message - fatal");
Log.Error(new NotImplementedException("test no message - error"), ex.Message);

我创建了以下 PR https://github.com/ServiceStack/ServiceStack/pull/1210因此,当单独使用异常对象进行调用时,服务堆栈将正确地将异常对象传递给 NLog。

关于azure - NLog with Application Insights - 将异常记录为异常而不是跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58829416/

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