Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog" Version="4.4.0" />
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="10.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />



</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Serilog;
using Serilog.Core;
using Serilog.Events;

var configuration = new ConfigurationBuilder()
Expand All @@ -10,13 +11,17 @@
Log.Information("Web Host started");
var builder = WebApplication.CreateBuilder(args);

// Wrapping the minimum level in a LoggingLevelSwitch lets us change it at
// runtime -- e.g. from an admin endpoint -- with no redeploy and no restart.
var levelSwitch = new LoggingLevelSwitch(LogEventLevel.Information);

builder.Services.AddSerilog(options =>
{
//we can configure serilog from configuration
options.ReadFrom.Configuration(configuration);

//or we can configure serilog via fluent api
options.MinimumLevel.Information()
options.MinimumLevel.ControlledBy(levelSwitch)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Information,
Expand Down Expand Up @@ -44,4 +49,12 @@
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

// e.g. POST /admin/log-level/Debug -- flips the minimum level on every sink
// immediately, with no restart.
app.MapPost("/admin/log-level/{level}", (LogEventLevel level) =>
{
levelSwitch.MinimumLevel = level;
return Results.Ok($"Minimum log level set to {level}.");
});

app.Run();
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
Loading