Skip to content

Commit

Permalink
Sample targeting
Browse files Browse the repository at this point in the history
  • Loading branch information
nblumhardt committed Jun 7, 2024
1 parent fa07aaf commit c608cc9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 73 deletions.
2 changes: 1 addition & 1 deletion sample/ConsoleDemo/ConsoleDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
46 changes: 19 additions & 27 deletions sample/ConsoleDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,29 @@
using System;
using System.Threading;

namespace ConsoleDemo;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console(theme: AnsiConsoleTheme.Code)
.CreateLogger();

public static class Program
try
{
public static void Main()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console(theme: AnsiConsoleTheme.Code)
.CreateLogger();
Log.Debug("Getting started");

try
{
Log.Debug("Getting started");
Log.Information("Hello {Name} from thread {ThreadId}", Environment.GetEnvironmentVariable("USERNAME"), Thread.CurrentThread.ManagedThreadId);

Log.Information("Hello {Name} from thread {ThreadId}", Environment.GetEnvironmentVariable("USERNAME"), Thread.CurrentThread.ManagedThreadId);
Log.Warning("No coins remain at position {@Position}", new { Lat = 25, Long = 134 });

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

Fail();
}
catch (Exception e)
{
Log.Error(e, "Something went wrong");
}
Fail();
}
catch (Exception e)
{
Log.Error(e, "Something went wrong");
}

Log.CloseAndFlush();
}
await Log.CloseAndFlushAsync();

static void Fail()
{
throw new DivideByZeroException();
}
}
static void Fail()
{
throw new DivideByZeroException();
}
79 changes: 35 additions & 44 deletions sample/SyncWritesDemo/Program.cs
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);
});
}
2 changes: 1 addition & 1 deletion sample/SyncWritesDemo/SyncWritesDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit c608cc9

Please sign in to comment.