Created
May 31, 2022 12:31
-
-
Save Platonenkov/b6c43f90c751f9025436061cd5039ab5 to your computer and use it in GitHub Desktop.
Serilog Элементарная настройка
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//....... | |
using Serilog; | |
using ILogger = Microsoft.Extensions.Logging.ILogger; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Logging; | |
//....... | |
public static ILogger Logger; | |
//....... | |
public static IHostBuilder CreateHostBuilder(string[] args) => Host | |
.CreateDefaultBuilder(args) | |
.UseSerilog((context, services, configuration) => configuration | |
.ReadFrom.Configuration(context.Configuration) | |
.ReadFrom.Services(services) | |
.Enrich.FromLogContext() | |
//.WriteTo.Console() | |
) | |
.ConfigureServices(ConfigureServices); | |
protected override async void OnStartup(StartupEventArgs e) | |
{ | |
var host = Hosting; | |
Logger = Services.GetRequiredService<ILogger<App>>(); | |
Logger.LogInformation("......."); | |
//........... | |
base.OnStartup(e); | |
await host.StartAsync(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Serilog": { | |
"MinimumLevel": { | |
"Default": "Information", | |
"Override": { | |
"Microsoft": "Warning", | |
"Microsoft.Hosting.Lifetime": "Information" | |
} | |
}, | |
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], | |
"WriteTo": [ | |
{ | |
"Name": "File", | |
"Args": { | |
"path": "./logs/txt/log_Statistics_.txt", | |
"rollingInterval": "Day" | |
} | |
}, | |
{ | |
"Name": "File", | |
"Args": { | |
"path": "./logs/json/log-.json", | |
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog" | |
} | |
} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ItemGroup> | |
<PackageReference Include="Serilog" Version="2.11.0" /> | |
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" /> | |
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" /> | |
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> | |
</ItemGroup> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment