[英]ASP.NET Core 2.0 using Serilog to log stacktrace when exception is thrown
So I've recently started to build a asp.net core application and for the logging i'm using SeriLog. 所以我最近开始构建一个asp.net核心应用程序,并且我正在使用SeriLog进行日志记录。 This was working fine until recently I found out that most of the time the stacktrace of an exception is not being transferred to my logs.
这工作正常,直到最近我发现大多数情况下异常的堆栈跟踪没有转移到我的日志。 I'm using the .WriteTo.RollingFile() method to write to a .txt file in my LoggerConfiguration in Startup.cs like so
我正在使用.WriteTo.RollingFile()方法在Startup.cs中的LoggerConfiguration中写入.txt文件,如此
public void ConfigureServices(IServiceCollection services)
{
//add a bunch of services
services.AddLogging(builder =>
{
builder.AddConsole();
builder.AddDebug();
var logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.WithExceptionDetails()
.WriteTo.RollingFile(Configuration.GetValue<string>("LogFilePath") + "-{Date}.txt", LogEventLevel.Information,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}) {Message}{NewLine}{Exception}")
.CreateLogger();
builder.AddSerilog(logger);
});
services.AddMvc();
}
And in my loggerFactory I added the Serilog with this line of code 在我的loggerFactory中,我添加了这行代码的Serilog
loggerFactory.AddSerilog();
My BuildWebHost method does not have the .UserSerilog() and looks like this: 我的BuildWebHost方法没有.UserSerilog(),如下所示:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
This method gets called as the last step in my Main
method in Program.cs Reading Serilog's documentation the {Exception} in the outputTemplate of the RollingFile should also log the stacktrace of the exception. 这个方法被调用作为Program.cs中我的
Main
方法的最后一步。阅读Serilog的文档,RollingFile的outputTemplate中的{Exception}也应该记录异常的堆栈跟踪。 However when for example I log an error like so (using the Microsoft.Extensions.Logging.ILogger
) 但是,例如我记录这样的错误(使用
Microsoft.Extensions.Logging.ILogger
)
_log.LogError("Exception thrown when trying to convert customer viewmodel to model or getting data from the database with id: " + id, ex);
This logs: 这个日志:
2017-12-12 10:59:46.871 +01:00 [Error] (ProjectName.Controllers.CustomersController) Exception thrown when trying to convert customer viewmodel to model or getting data from the database with id: 137dfdc1-6a96-4621-106c-08d538a26c5b
It does not have a stacktrace. 它没有堆栈跟踪。 But when for example I forget to inject a class into the constructor of a class through constructor injection from my .addServices it does log the stacktrace.
但是,例如,当我忘记通过我的.addServices中的构造函数注入将类注入到类的构造函数中时,它会记录堆栈跟踪。 For example:
例如:
2017-12-12 11:03:23.968 +01:00 [Error] (Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware) An unhandled exception has occurred while executing the request
System.InvalidOperationException: Unable to resolve service for type 'TypeName' while attempting to activate 'ProjectName.Controllers.CustomersController'.
at Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method(Closure , IServiceProvider , Object[] )
How do I get the stacktrace to show up in my logging .txt file? 如何让stacktrace显示在我的日志.txt文件中?
LogError
extension method has following overrides: LogError
扩展方法具有以下覆盖:
public static void LogError(this ILogger logger, Exception exception, string message, params object[] args);
public static void LogError(this ILogger logger, string message, params object[] args);
When you call 你打电话的时候
_log.LogError("Exception thrown when trying to convert customer viewmodel to model or getting data from the database with id: " + id, ex);
you actually use the second one and ex
object is passed just as parameter for formatting. 你实际上使用第二个和
ex
对象作为格式参数传递。 As far as your message does not have formatting items, passed exception is just ignored. 只要您的消息没有格式化项,就会忽略传递的异常。
To fix the problem just switch arguments in your call, exception should be the first: 要解决问题,只需在调用中切换参数,异常应该是第一个:
_log.LogError(ex, "Exception thrown when trying to convert customer viewmodel to model or getting data from the database with id: " + id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.