问题
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.
回答1:
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.
回答2:
I can log it as Exception instead of Trace via Log.Fatal(new NotImplementedException("test not implemented exception"));
The nuget package I installed for .net core web project:
Microsoft.ApplicationInsights.NLogTarget, version 2.11.0
Microsoft.ApplicationInsights.AspNetCore, version 2.8.2
Here is the NLog.config
in my project:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="Microsoft.ApplicationInsights.NLogTarget" />
</extensions>
<targets>
<target xsi:type="ApplicationInsightsTarget" name="aiTarget">
<!--<instrumentationKey>Your_Resource_Key</instrumentationKey>-->
<!-- Only required if not using ApplicationInsights.config -->
<contextproperty name="threadid" layout="${threadid}" />
<!-- Can be repeated with more context -->
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="aiTarget" />
</rules>
</nlog>
And in HomeController.cs -> Index() method:
public IActionResult Index()
{
Logger logger = LogManager.GetLogger("HomeController");
logger.Fatal(new NotImplementedException("test not implemented exception, only one new exception() 222"));
return View();
}
After execution the code, in azure portal -> application insights -> Search, this logged message is shown under Exception. Screenshot as below:
来源:https://stackoverflow.com/questions/58829416/nlog-with-application-insights-logging-exceptions-as-an-exception-instead-of-a