如何在serilog中自定义日志文件
How to customise log file in serilog
这是我的 serilog 的输出。
2022-05-24T16:31:13.4340853+05:30 [INF] Application started. Press Ctrl+C to shut down.
(dcaefe54)
2022-05-24T16:31:13.5439262+05:30 [INF] Hosting environment: "Development" (c3307c92)
2022-05-24T16:31:13.5468423+05:30 [INF] Content root path:
"E:\Projects\Eigen\Eigen.UE.WebUI.Razor" (b5d60022)
2022-05-24T16:31:19.0392934+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 1
record (d1092e14)
2022-05-24T16:31:19.0761232+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 2
record (9758a16f)
2022-05-24T16:31:19.0786689+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 3
record (ddc237d7)
2022-05-24T16:31:19.0816914+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 4
record (ca210324)
2022-05-24T16:31:19.0851599+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 5
record (d2fab210)
我不明白这种 guid 在 8000000e-0000-fd00-b63f-84710c7967bb 中的位置,如何删除这个 guid?
这是我的 startup.cs 文件
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
loggerFactory.AddFile("Logs/EigenLog-{Date}.txt");
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});
}
GUID 是请求 ID:
https://github.com/serilog/serilog-extensions-logging-file/#file-format
要对其进行自定义,请指定输出模板:
loggerFactory.AddFile("Logs/EigenLog-{Date}.txt", outputTemplate:
"{Timestamp:o} [{Level:u3}] {Message} {Properties:j} ({EventId:x8}){NewLine}{Exception}"
);
这是我的 serilog 的输出。
2022-05-24T16:31:13.4340853+05:30 [INF] Application started. Press Ctrl+C to shut down.
(dcaefe54)
2022-05-24T16:31:13.5439262+05:30 [INF] Hosting environment: "Development" (c3307c92)
2022-05-24T16:31:13.5468423+05:30 [INF] Content root path:
"E:\Projects\Eigen\Eigen.UE.WebUI.Razor" (b5d60022)
2022-05-24T16:31:19.0392934+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 1
record (d1092e14)
2022-05-24T16:31:19.0761232+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 2
record (9758a16f)
2022-05-24T16:31:19.0786689+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 3
record (ddc237d7)
2022-05-24T16:31:19.0816914+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 4
record (ca210324)
2022-05-24T16:31:19.0851599+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 5
record (d2fab210)
我不明白这种 guid 在 8000000e-0000-fd00-b63f-84710c7967bb 中的位置,如何删除这个 guid?
这是我的 startup.cs 文件
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
loggerFactory.AddFile("Logs/EigenLog-{Date}.txt");
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});
}
GUID 是请求 ID:
https://github.com/serilog/serilog-extensions-logging-file/#file-format
要对其进行自定义,请指定输出模板:
loggerFactory.AddFile("Logs/EigenLog-{Date}.txt", outputTemplate:
"{Timestamp:o} [{Level:u3}] {Message} {Properties:j} ({EventId:x8}){NewLine}{Exception}"
);