From ec7fd0f6f6770548614b12ac99e630e84866e19a Mon Sep 17 00:00:00 2001 From: wenjy Date: Thu, 26 Oct 2023 16:04:08 +0800 Subject: [PATCH] add - Serilog --- .../Controllers/WeatherForecastController.cs | 1 + Durkee.Mes.Api/Durkee.Mes.Api.csproj | 4 +++ Durkee.Mes.Api/Program.cs | 10 +++++--- Durkee.Mes.Api/Startup.cs | 25 ++++++++++++++----- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/Durkee.Mes.Api/Controllers/WeatherForecastController.cs b/Durkee.Mes.Api/Controllers/WeatherForecastController.cs index 8f11e65..8eb7392 100644 --- a/Durkee.Mes.Api/Controllers/WeatherForecastController.cs +++ b/Durkee.Mes.Api/Controllers/WeatherForecastController.cs @@ -34,6 +34,7 @@ namespace Durkee.Mes.Api.Controllers Summary = Summaries[rng.Next(Summaries.Length)] }) .ToArray(); + } } } diff --git a/Durkee.Mes.Api/Durkee.Mes.Api.csproj b/Durkee.Mes.Api/Durkee.Mes.Api.csproj index 4944084..1bcb835 100644 --- a/Durkee.Mes.Api/Durkee.Mes.Api.csproj +++ b/Durkee.Mes.Api/Durkee.Mes.Api.csproj @@ -2,9 +2,13 @@ netcoreapp3.1 + + true + + diff --git a/Durkee.Mes.Api/Program.cs b/Durkee.Mes.Api/Program.cs index 494565b..d7914eb 100644 --- a/Durkee.Mes.Api/Program.cs +++ b/Durkee.Mes.Api/Program.cs @@ -1,11 +1,10 @@ using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Events; using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.IO; namespace Durkee.Mes.Api { @@ -14,10 +13,13 @@ namespace Durkee.Mes.Api public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); + + Log.CloseAndFlush(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) + .UseSerilog() .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); diff --git a/Durkee.Mes.Api/Startup.cs b/Durkee.Mes.Api/Startup.cs index 5fbe708..75650c1 100644 --- a/Durkee.Mes.Api/Startup.cs +++ b/Durkee.Mes.Api/Startup.cs @@ -1,16 +1,13 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.HttpsPolicy; -using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; +using Serilog; +using Serilog.Events; using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.IO; namespace Durkee.Mes.Api { @@ -53,6 +50,20 @@ namespace Durkee.Mes.Api //c.DisplayRequestDuration(); // 显示请求持续时间(可选) }); + app.UseSerilogRequestLogging(); + //Log.Logger = new LoggerConfiguration() + // .MinimumLevel.Information().WriteTo.Console() + // .WriteTo.File($"Logs/{DateTime.UtcNow:yyyyMMdd}/.txt", rollingInterval: RollingInterval.Day) + // .CreateLogger(); + var logPath = $"E:/代码生成/日志信息/Logs/{DateTime.UtcNow:yyyy-MM-dd}/"; + Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.Console() + .WriteTo.File(Path.Combine(logPath, "Info.log"), LogEventLevel.Information, fileSizeLimitBytes: 5*1024) + .WriteTo.File(Path.Combine(logPath, "Error.log"), LogEventLevel.Error, fileSizeLimitBytes: 5 * 1024) + .WriteTo.File(Path.Combine(logPath, "Warn.log"), LogEventLevel.Warning, fileSizeLimitBytes: 5 * 1024) + .WriteTo.File(Path.Combine(logPath, "Trace.log"), LogEventLevel.Verbose, fileSizeLimitBytes: 5 * 1024) + .CreateLogger(); + app.UseSerilogRequestLogging(); + //app.UseHttpsRedirection(); app.UseRouting(); @@ -63,6 +74,8 @@ namespace Durkee.Mes.Api { endpoints.MapControllers(); }); + + Log.Information($"项目初始化完成,日志存放路径:{logPath}"); } } }