Skip to content

Commit 1da67f0

Browse files
committed
Use LevelSwitch in sample app
1 parent 0333c21 commit 1da67f0

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

sample/CustomLogEventFormatterDemo/Program.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading;
33
using Serilog;
4+
using Serilog.Core;
45
using Serilog.Sinks.MSSqlServer;
56

67
namespace CustomLogEventFormatterDemo
@@ -16,6 +17,7 @@ public static void Main()
1617
var options = new ColumnOptions();
1718
options.Store.Add(StandardColumn.LogEvent);
1819
var customFormatter = new FlatLogEventFormatter();
20+
var levelSwitch = new LoggingLevelSwitch();
1921

2022
// Legacy interace - do not use this anymore
2123
//Log.Logger = new LoggerConfiguration()
@@ -47,7 +49,8 @@ public static void Main()
4749
formatProvider: null,
4850
columnOptions: options,
4951
columnOptionsSection: null,
50-
logEventFormatter: customFormatter)
52+
logEventFormatter: customFormatter,
53+
levelSwitch: levelSwitch)
5154
.CreateLogger();
5255

5356
try
@@ -58,6 +61,8 @@ public static void Main()
5861

5962
Log.Warning("No coins remain at position {@Position}", new { Lat = 25, Long = 134 });
6063

64+
UseLevelSwitchToModifyLogLevelDuringRuntime(levelSwitch);
65+
6166
Fail();
6267
}
6368
catch (DivideByZeroException e)
@@ -68,6 +73,19 @@ public static void Main()
6873
Log.CloseAndFlush();
6974
}
7075

76+
private static void UseLevelSwitchToModifyLogLevelDuringRuntime(LoggingLevelSwitch levelSwitch)
77+
{
78+
levelSwitch.MinimumLevel = Serilog.Events.LogEventLevel.Error;
79+
80+
Log.Information("This should not be logged");
81+
82+
Log.Error("This should be logged");
83+
84+
levelSwitch.MinimumLevel = Serilog.Events.LogEventLevel.Information;
85+
86+
Log.Information("This should be logged again");
87+
}
88+
7189
private static void Fail()
7290
{
7391
throw new DivideByZeroException();

0 commit comments

Comments
 (0)