我相信您做得很好,我是第一次使用Serilogs。我使用ASP CORE 3.0。我注意到,Serilogs仅在停止应用程序后记录事件,而不会记录实时事件。
我的appsettings.Development.json
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"System": "Warning",
"Microsoft": "Information",
"Microsoft.AspNetCore": "Information"
},
"File": {
"location": "logs/logging_api.log"
}};
Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseSerilog((hostingContext, loggerConfiguration) =>
{
// About log messges formatting: https://github.com/serilog/serilog/wiki/Formatting-Output#formatting-plain-text
// Implement serilog configurations
loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {HttpContext}");
var logFileLocation = hostingContext.Configuration.GetSection("Serilog:File:Location").Value ??
hostingContext.Configuration.GetSection("LOG_FILE_LOCATION").Value;
if (logFileLocation != null)
{
loggerConfiguration
.WriteTo.File(
logFileLocation,
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {HttpContext}",
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true,
fileSizeLimitBytes: 50 * 1024 * 1024);
}
});
Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSerilogRequestLogging();
// Enable default healthcheck
app.UseHealthChecks("/api/health/check");
// Enable swagger only for development environments
// specifying the Swagger JSON endpoint.
if (this.configuration.GetValue<bool>("EnableSwagger"))
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Logging API V1");
});
}
app.UseRouting();
// app.UseMiddleware<LoggingApiAuth>();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
项目文件
<ProjectReference Include="..\LoggingApi.Data\LoggingApi.Data.csproj" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0-rc4" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="5.0.0-rc4" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
<PackageReference Include="Serilog.Enrichers.AspnetcoreHttpcontext" Version="1.1.0" />
问题:
仅在我停止应用程序后记录
Loaded **'/usr/local/share/dotnet/shared/Microsoft.NETCore.App/3.0.0/System.Net.NameResolution.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
程序“ [42520] LoggingApi.Edge.dll”已退出,代码为0(0x0)**。
[18:06:57 INF]现在正在收听:http://localhost:5000 [18:06:57 INF]请求启动HTTP / 1.1 GET http://localhost:5000/ [18:06:57 INF]应用程序已启动。按Ctrl + C关闭。 [18:06:57 INF]宿主环境:开发[18:06:57 INF]内容根路径:/development/logging-api/LoggingApi.Edge [18:06:57 INF] HTTP GET /在101.3370毫秒内响应404 [18:06:57 INF]请求在795.6172ms内完成404 [18:06:59 INF]请求开始HTTP / 1.1 GET http://localhost:5000/ [18:06:59 INF] HTTP GET /在1.1243毫秒内响应了404 [ 18:06:59 INF]请求在3.2939000000000003ms中完成404 [18:07:31 INF]请求启动HTTP / 1.1 GET http://localhost:5000/api/actionlogs/internal/5db0f148ba2a6056da8463e5 application / json [18:07:31 INF]执行端点'LoggingApi.Edge。 Controllers.ActionLogsController.GetByIdAsync(LoggingApi.Edge)'[18:07:31 INF]路由与{action =“ GetById”,controller =“ ActionLogs”}匹配。使用签名System.Threading.Tasks.Task`1 [Microsoft.AspNetCore.Mvc.IActionResult]控制器LoggingApi.Edge.Controllers.ActionLogsController(LoggingApi.Edge)上的GetByIdAsync(System.String)执行控制器操作。 [18:07:32 INF]执行ObjectResult,写入类型为'LoggingApi.Data.Models.ActionLog'的值。 [18:07:32 INF]在973.6433000000001ms中执行了操作LoggingApi.Edge.Controllers.ActionLogsController.GetByIdAsync(LoggingApi.Edge)[18:07:32 INF]执行了端点'LoggingApi.Edge.Controllers.ActionLogsController.GetByIdAsync(LoggingApi。 Edge)'[18:07:32 INF] HTTP GET / api / actionlogs / internal / 5db0f148ba2a6056da8463e5在1067.5599毫秒内响应了200 [18:07:32 INF]在1069.1802ms 200 application / json中完成请求; charset = utf-8