public void ViaTextLogFile() { ConfigureLogger.StandardSetup(); var key = "CORRELATION_ID"; var staticValueKey = "STATIC"; var staticValue = "this value should be in all logs"; LoggingContext.Set(staticValueKey, staticValue); Parallel.For(1, 10000, (x) => { LoggingContext.Set(key, x); this.GetLogger().LogWarningWithContext("current number processing: {Number}", x); }); Log.CloseAndFlush(); var logFile = Directory.EnumerateFiles("./", "log*.txt").First(); string source = this.GetType().ToString(); var lines = File.ReadLines(logFile); foreach (string line in lines.Skip(1)) //skip the first line because it is expected to be different { var obj = JObject.Parse(line); Assert.StrictEqual(obj["Number"], obj[LoggerExtensions.LOG_KEY_CONTEXT][key]); Assert.StrictEqual(source, obj[LoggerExtensions.LOG_KEY_CONTEXT][LoggerExtensions.LOG_KEY_SOURCE]); Assert.StrictEqual(staticValue, obj[LoggerExtensions.LOG_KEY_CONTEXT][staticValueKey]); } File.Delete(logFile); }
public static void RunBackgroundTaskWorker <TWorker>(RunBackgroundTaskWorkerParams runBackgroundTaskWorkerParams) where TWorker : class, IHostedService { ConfigureLogger.StandardSetup(logLevel: runBackgroundTaskWorkerParams.LogLevel); Host.CreateDefaultBuilder(runBackgroundTaskWorkerParams.Args) .UseSystemd() .ConfigureServices((hostContext, services) => { runBackgroundTaskWorkerParams.IocDelegate(hostContext, null, services); services.AddSingleton(provider => runBackgroundTaskWorkerParams.WorkerProcess); services.AddSingleton(provider => runBackgroundTaskWorkerParams.IocDelegate); services.AddHostedService <TWorker>(); }) .Build().Run(); }
public static void RunCustomWorker <TWorker>(RunCustomWorkerParams runCustomWorkerParams) where TWorker : class, IHostedService { ConfigureLogger.StandardSetup(logLevel: runCustomWorkerParams.LogLevel); Host.CreateDefaultBuilder(runCustomWorkerParams.Args) .UseWindowsService() .ConfigureServices((hostContext, services) => { runCustomWorkerParams.IocDelegate(hostContext, null, services); services.AddSingleton(provider => runCustomWorkerParams.ListenerInfo); services.AddSingleton(provider => runCustomWorkerParams.IocDelegate); services.AddSingleton(provider => runCustomWorkerParams.WebAppBuilderDelegate); services.AddHostedService <TWorker>(); }) .Build().Run(); }
public static void Main(string[] args) { ConfigureLogger.StandardSetup(() => CreateHostBuilder(args).Build().Run()); }