add - Serilog

master
wenjy 1 year ago
parent 9b7fafebc6
commit ec7fd0f6f6

@ -34,6 +34,7 @@ namespace Durkee.Mes.Api.Controllers
Summary = Summaries[rng.Next(Summaries.Length)] Summary = Summaries[rng.Next(Summaries.Length)]
}) })
.ToArray(); .ToArray();
} }
} }
} }

@ -2,9 +2,13 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<!-- Other project properties -->
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup> </ItemGroup>

@ -1,11 +1,10 @@
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using System; using System;
using System.Collections.Generic; using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Durkee.Mes.Api namespace Durkee.Mes.Api
{ {
@ -14,10 +13,13 @@ namespace Durkee.Mes.Api
public static void Main(string[] args) public static void Main(string[] args)
{ {
CreateHostBuilder(args).Build().Run(); CreateHostBuilder(args).Build().Run();
Log.CloseAndFlush();
} }
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder => .ConfigureWebHostDefaults(webBuilder =>
{ {
webBuilder.UseStartup<Startup>(); webBuilder.UseStartup<Startup>();

@ -1,16 +1,13 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Serilog;
using Serilog.Events;
using System; using System;
using System.Collections.Generic; using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Durkee.Mes.Api namespace Durkee.Mes.Api
{ {
@ -53,6 +50,20 @@ namespace Durkee.Mes.Api
//c.DisplayRequestDuration(); // 显示请求持续时间(可选) //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.UseHttpsRedirection();
app.UseRouting(); app.UseRouting();
@ -63,6 +74,8 @@ namespace Durkee.Mes.Api
{ {
endpoints.MapControllers(); endpoints.MapControllers();
}); });
Log.Information($"项目初始化完成,日志存放路径:{logPath}");
} }
} }
} }

Loading…
Cancel
Save