比如对于同样的错误日志,控制台中是这么显示的:
fail: GeetestSDK.GeetestLib[0] Server regist challenge failed!
而 Serilog 日志文件中是这么显示的:
2017-08-06 18:59:15.603 +08:00 [Error] Server regist challenge failed!
请问如何让 Serilog 也显示 GeetestSDK.GeetestLib[0] 信息?
Serilog.Sinks.Console 默认的 outputTemplate 是 "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
,在其中添加 {SourceContext}
即可,示例代码如下:
class Program
{
static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console(outputTemplate:
"{Timestamp:HH:mm:ss} [{Level}] {SourceContext} {NewLine}{Message}{NewLine}{Exception}")
.CreateLogger();
var webHost = WebHost.CreateDefaultBuilder(args)
.Configure(_ => { })
.UseSerilog()
.Build();
var logger = webHost.Services.GetService<ILoggerFactory>().CreateLogger<Program>();
logger.LogError("an error occured");
//Output:
//20:47:21 [Error] Q92475.Program
//an error occured
}
}