|
| 1 | +using System.Reflection; |
| 2 | +using Microsoft.Extensions.Configuration; |
| 3 | +using Serilog; |
| 4 | +using Serilog.Debugging; |
| 5 | +using Serilog.Settings.Configuration; |
| 6 | + |
| 7 | +if (args.Length == 1 && args[0] == "is-single-file") |
| 8 | +{ |
| 9 | + if (typeof(Program).Assembly.GetManifestResourceNames().Any(e => e.StartsWith("costura."))) |
| 10 | + { |
| 11 | + Console.WriteLine(true); |
| 12 | + return 0; |
| 13 | + } |
| 14 | + // IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app |
| 15 | +#pragma warning disable IL3000 |
| 16 | + Console.WriteLine(string.IsNullOrEmpty(Assembly.GetEntryAssembly()?.Location)); |
| 17 | +#pragma warning restore |
| 18 | + return 0; |
| 19 | +} |
| 20 | + |
| 21 | +SelfLog.Enable(Console.Error); |
| 22 | + |
| 23 | +Thread.CurrentThread.Name = "Main thread"; |
| 24 | +const string outputTemplate = "({ThreadName}) [{Level}] {Message}{NewLine}"; |
| 25 | + |
| 26 | +var configurationValues = new Dictionary<string, string?>(); |
| 27 | +var minimumLevelOnly = args.Contains("--minimum-level-only"); |
| 28 | +if (minimumLevelOnly) |
| 29 | +{ |
| 30 | + configurationValues["Serilog:MinimumLevel"] = "Verbose"; |
| 31 | +} |
| 32 | +else |
| 33 | +{ |
| 34 | + configurationValues["Serilog:Enrich:0"] = "WithThreadName"; |
| 35 | + configurationValues["Serilog:WriteTo:0:Name"] = "Console"; |
| 36 | + configurationValues["Serilog:WriteTo:0:Args:outputTemplate"] = outputTemplate; |
| 37 | +} |
| 38 | + |
| 39 | +if (args.Contains("--using-thread")) configurationValues["Serilog:Using:Thread"] = "Serilog.Enrichers.Thread"; |
| 40 | +if (args.Contains("--using-console")) configurationValues["Serilog:Using:Console"] = "Serilog.Sinks.Console"; |
| 41 | + |
| 42 | +var assemblies = new List<Assembly>(); |
| 43 | +if (args.Contains("--assembly-thread")) assemblies.Add(typeof(ThreadLoggerConfigurationExtensions).Assembly); |
| 44 | +if (args.Contains("--assembly-console")) assemblies.Add(typeof(ConsoleLoggerConfigurationExtensions).Assembly); |
| 45 | + |
| 46 | +try |
| 47 | +{ |
| 48 | + var configuration = new ConfigurationBuilder().AddInMemoryCollection(configurationValues).Build(); |
| 49 | + var options = assemblies.Count > 0 ? new ConfigurationReaderOptions(assemblies.ToArray()) : null; |
| 50 | + var loggerConfiguration = new LoggerConfiguration().ReadFrom.Configuration(configuration, options); |
| 51 | + if (minimumLevelOnly) |
| 52 | + { |
| 53 | + loggerConfiguration |
| 54 | + .Enrich.WithThreadName() |
| 55 | + .WriteTo.Console(outputTemplate: outputTemplate); |
| 56 | + } |
| 57 | + var logger = loggerConfiguration.CreateLogger(); |
| 58 | + logger.Information("Expected success"); |
| 59 | + return 0; |
| 60 | +} |
| 61 | +catch (InvalidOperationException exception) when (exception.Message.StartsWith("No Serilog:Using configuration section is defined and no Serilog assemblies were found.")) |
| 62 | +{ |
| 63 | + Console.WriteLine("Expected exception"); |
| 64 | + return 0; |
| 65 | +} |
| 66 | +catch (Exception exception) |
| 67 | +{ |
| 68 | + Console.Error.WriteLine(exception); |
| 69 | + return 1; |
| 70 | +} |
0 commit comments