当前,我正在使用通过Service Stack构建的.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-此内容未提及如何将类型从“跟踪”更改为“异常”
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"));
结果如下:
编辑2:程序包版本如下:
NLog (4.6.8)
NLog.Web.AspNetCore (4.9.0)
microsoft.ApplicationInsights.AspNetCore (2.8.2)
microsoft.ApplicationInsights.NLogTarget (2.11.0)
编辑3:更多代码:
我们的服务接口层使用此代码。它实现了Service Stack所提供的服务类。通过如上所述定义LogFactory,我们可以将其传递到我们的服务层。
此服务接口托管在一个位于同一解决方案下的单独项目中(我们将其称为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}!" };
}
}
我们使用以下网址调用此函数:
它所做的只是返回一个文本,上面写着“ Hola,name!”
不幸的是,我无法附加调试器-出于任何原因,我都无法使其达到断点。
我不知道也许我是否也需要在Api.ServiceInterface项目上拥有相同的库。请注意,Api.ServiceInterface是类库类型的项目,并且在.NET Standard 2.0上运行。