-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa07aaf
commit c608cc9
Showing
4 changed files
with
56 additions
and
73 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,47 @@ | ||
using Serilog; | ||
using Serilog.Sinks.SystemConsole.Themes; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SyncWritesDemo; | ||
Console.WriteLine("A sample of how to sync writes to the console sink."); | ||
|
||
public static class Program | ||
if (args is { Length: 1 }) | ||
{ | ||
public static void Main(string[] args) | ||
switch (args[0]) | ||
{ | ||
Console.WriteLine("A sample of how to sync writes to the console sink."); | ||
|
||
if (args != null && args.Length == 1) | ||
{ | ||
switch (args[0]) | ||
{ | ||
case "--sync-root-default": | ||
SystemConsoleSyncTest(syncRootForLogger1: null, syncRootForLogger2: null); | ||
return; | ||
case "--sync-root-separate": | ||
SystemConsoleSyncTest(syncRootForLogger1: new object(), syncRootForLogger2: new object()); | ||
return; | ||
case "--sync-root-same": | ||
var sameSyncRoot = new object(); | ||
SystemConsoleSyncTest(syncRootForLogger1: sameSyncRoot, syncRootForLogger2: sameSyncRoot); | ||
return; | ||
} | ||
} | ||
|
||
Console.WriteLine("Expecting one of the following arguments:{0}--sync-root-default{0}--sync-root-separate{0}--sync-root-same", Environment.NewLine); | ||
case "--sync-root-default": | ||
SystemConsoleSyncTest(syncRootForLogger1: null, syncRootForLogger2: null); | ||
return; | ||
case "--sync-root-separate": | ||
SystemConsoleSyncTest(syncRootForLogger1: new object(), syncRootForLogger2: new object()); | ||
return; | ||
case "--sync-root-same": | ||
var sameSyncRoot = new object(); | ||
SystemConsoleSyncTest(syncRootForLogger1: sameSyncRoot, syncRootForLogger2: sameSyncRoot); | ||
return; | ||
} | ||
} | ||
|
||
static void SystemConsoleSyncTest(object syncRootForLogger1, object syncRootForLogger2) | ||
{ | ||
var logger1 = new LoggerConfiguration() | ||
.MinimumLevel.Verbose() | ||
.Enrich.WithProperty("Logger", "logger1") | ||
.WriteTo.Console(theme: SystemConsoleTheme.Literate, syncRoot: syncRootForLogger1) | ||
.CreateLogger(); | ||
Console.WriteLine("Expecting one of the following arguments:{0}--sync-root-default{0}--sync-root-separate{0}--sync-root-same", Environment.NewLine); | ||
|
||
var logger2 = new LoggerConfiguration() | ||
.MinimumLevel.Verbose() | ||
.Enrich.WithProperty("Logger", "logger2") | ||
.WriteTo.Console(theme: SystemConsoleTheme.Literate, syncRoot: syncRootForLogger2) | ||
.CreateLogger(); | ||
static void SystemConsoleSyncTest(object syncRootForLogger1, object syncRootForLogger2) | ||
{ | ||
var logger1 = new LoggerConfiguration() | ||
.MinimumLevel.Verbose() | ||
.Enrich.WithProperty("Logger", "logger1") | ||
.WriteTo.Console(theme: SystemConsoleTheme.Literate, syncRoot: syncRootForLogger1) | ||
.CreateLogger(); | ||
|
||
var options = new ParallelOptions { MaxDegreeOfParallelism = 8 }; | ||
Parallel.For(0, 1000, options, (i, loopState) => | ||
{ | ||
var logger = (i % 2 == 0) ? logger1 : logger2; | ||
logger.Information("Event {Iteration} generated by {ThreadId}", i, Thread.CurrentThread.ManagedThreadId); | ||
}); | ||
} | ||
} | ||
var logger2 = new LoggerConfiguration() | ||
.MinimumLevel.Verbose() | ||
.Enrich.WithProperty("Logger", "logger2") | ||
.WriteTo.Console(theme: SystemConsoleTheme.Literate, syncRoot: syncRootForLogger2) | ||
.CreateLogger(); | ||
|
||
var options = new ParallelOptions { MaxDegreeOfParallelism = 8 }; | ||
Parallel.For(0, 1000, options, (i, _) => | ||
{ | ||
var logger = i % 2 == 0 ? logger1 : logger2; | ||
logger.Information("Event {Iteration} generated by {ThreadId}", i, Environment.CurrentManagedThreadId); | ||
}); | ||
} |
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