具有Application Insights的NLog-将异常记录为异常而不是跟踪 [英] NLog with Application Insights - logging exceptions as an exception instead of a trace

查看:44
本文介绍了具有Application Insights的NLog-将异常记录为异常而不是跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用通过Service Stack构建的.NET Core Web应用程序.当前使用NLog并以Azure Application Insights为目标来提供日志记录.

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

  Log.Fatal(测试未实现的异常",新的NotImplementedException());//此日志记录在Application Insights中的例外"下Log.Fatal(new NotImplementedException(测试未实现的异常"));//此记录在跟踪下 

我希望能够将第二行记录为异常而不是跟踪.我有什么办法可以做到这一点?

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

程序包版本如下:

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

更多代码:

我们的服务接口层使用此代码.它实现了Service Stack所提供的服务类,该类由Service Stack提供.通过如上所述定义LogFactory,我们可以将其传递到我们的服务层.

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

AppServiceBase.cs

 公共抽象类AppServiceBase:服务{受保护的ILog日志{放;}受保护的ICacheClient CacheClient {get;放;}受保护的AppServiceBase(){日志= LogManager.GetLogger(GetType());}公共AppServiceBase(ICacheClient cacheClient):this(){CacheClient = cacheClient;}public UserSession UserSession =>this.GetSession()作为UserSession;} 

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

 公共类HelloService:AppServiceBase{公共对象Any(您好请求){Log.Fatal(测试无消息-致命1",新的NotImplementedException());Log.Error(new NotImplementedException("test no message-error 1"));返回新的HelloResponse {结果= $"Hola,{request.Name}!"};}} 

我们使用以下网址调用此函数:

http://localhost/hello/name

它所做的只是返回一个文本,上面写着"Hola,name!"

不幸的是,我无法附加调试器-出于任何原因,我都无法使其达到断点.

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

解决方案

注意已发布可解决此问题的ServiceStack 5.8.

查看了服务堆栈版本.5.7记录器接口.这将不起作用:

 //将通过string.Format参数传递异常Log.Fatal(测试无消息-致命",新的NotImplementedException());//将使服务堆栈调用Exception.ToStringLog.Error(new NotImplementedException("test no message-error"))); 

您需要采取以下解决方法才能正确访问NLog:

  Log.Fatal(new NotImplementedException(),测试无消息-致命");Log.Error(new NotImplementedException("test no message-error"),ex.Message); 

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

Currently I am using a .NET Core Web Application built using Service Stack. Logging is currently provided by using NLog, with Azure Application Insights as a target.

Currently when I log messages and exceptions, I am coming across some weird behaviours:

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

What I would like is to be able to log the second line as an Exception instead of a Trace. Is there any way I can achieve this?

I was not able to find an answer to this, so I am posting as a question.

NLog exceptions as Trace in Application Insights - this one does not mention how to change the type from Trace to Exception

Application Insights - Logging exceptions - this one does not mention NLog

https://github.com/Microsoft/ApplicationInsights-dotnet-logging/issues/102 - this is the closest to what I need, but there was no update on it

EDIT: Because commenting markup can't show images and formatting properly, I have been testing with the following lines of code:

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

The outcome is as belows:

EDIT 2: Package versions are as below:

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

EDIT 3: A bit more code:

Our service interface layer uses this code. It implements stuff off the Service class, which is supplied by Service Stack. By defining the LogFactory as above, we are able to have it pass down to our service layers.

This service interface is hosted in a separate project that sits under the same solution (let's call it 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 (a service that extends off AppServiceBase, and thus can directly call the logger in AppServiceBase above).

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}!" }; 
        }
    }

We call this function using the following URL:

http://localhost/hello/name

All it does is return a text that says "Hola, name!"

Unfortunately I cannot attach a debugger - for whatever reason, I cannot get it to hit a breakpoint.

I don't know if perhaps I need to have the same libraries on Api.ServiceInterface project as well. Note that Api.ServiceInterface is a project of type Class Library and runs on .NET Standard 2.0.

解决方案

NOTE ServiceStack 5.8 has been released that fixes the issue.

Have looked at Service Stack ver. 5.7 Logger Interface. And this will not work:

// 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"));

And you need to make this workaround to correctly reach NLog:

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

I have crated the following PR https://github.com/ServiceStack/ServiceStack/pull/1210 so Service Stack will correctly pass the Exception-object to NLog, when calling with exception-object alone.

这篇关于具有Application Insights的NLog-将异常记录为异常而不是跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆