1
1
using System ;
2
2
using System . Threading ;
3
3
using Serilog ;
4
+ using Serilog . Core ;
4
5
using Serilog . Sinks . MSSqlServer ;
5
6
6
7
namespace CustomLogEventFormatterDemo
@@ -16,6 +17,7 @@ public static void Main()
16
17
var options = new ColumnOptions ( ) ;
17
18
options . Store . Add ( StandardColumn . LogEvent ) ;
18
19
var customFormatter = new FlatLogEventFormatter ( ) ;
20
+ var levelSwitch = new LoggingLevelSwitch ( ) ;
19
21
20
22
// Legacy interace - do not use this anymore
21
23
//Log.Logger = new LoggerConfiguration()
@@ -47,7 +49,8 @@ public static void Main()
47
49
formatProvider : null ,
48
50
columnOptions : options ,
49
51
columnOptionsSection : null ,
50
- logEventFormatter : customFormatter )
52
+ logEventFormatter : customFormatter ,
53
+ levelSwitch : levelSwitch )
51
54
. CreateLogger ( ) ;
52
55
53
56
try
@@ -58,6 +61,8 @@ public static void Main()
58
61
59
62
Log . Warning ( "No coins remain at position {@Position}" , new { Lat = 25 , Long = 134 } ) ;
60
63
64
+ UseLevelSwitchToModifyLogLevelDuringRuntime ( levelSwitch ) ;
65
+
61
66
Fail ( ) ;
62
67
}
63
68
catch ( DivideByZeroException e )
@@ -68,6 +73,19 @@ public static void Main()
68
73
Log . CloseAndFlush ( ) ;
69
74
}
70
75
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
+
71
89
private static void Fail ( )
72
90
{
73
91
throw new DivideByZeroException ( ) ;
0 commit comments