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

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

问题描述

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

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

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

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

我找不到答案,所以我发帖提问.

编辑 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 类之外的东西,它由 Service Stack 提供.通过如上定义 LogFactory,我们可以将其传递给我们的服务层.

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

AppServiceBase.cs

公共抽象类 AppServiceBase : Service{受保护的 ILog 日志 { 获取;放;}受保护的 ICacheClient CacheClient { 获取;放;}受保护的 AppServiceBase(){Log = LogManager.GetLogger(GetType());}公共 AppServiceBase(ICacheClient cacheClient) : this(){缓存客户端 = 缓存客户端;}公共用户会话用户会话 =>this.GetSession() 作为 UserSession;}

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

 公共类 HelloService : AppServiceBase{公共对象 Any(Hello 请求){Log.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

它所做的只是返回一个文本,上面写着你好,名字!"

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

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

解决方案

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

看过服务堆栈版本.5.7 记录器界面.这行不通:

//将异常传递一个 string.Format 参数Log.Fatal("测试无消息 - 致命", new NotImplementedException());//将使服务栈调用 Exception.ToStringLog.Error(new NotImplementedException("测试无消息-错误"));

并且您需要使用此解决方法才能正确访问 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.

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

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