static void Main(string[] args) { var clusterConfiguration = new EphemeralClusterConfiguration("7.9.0"); using var cluster = new EphemeralCluster(clusterConfiguration); cluster.Start(TimeSpan.FromMinutes(2)); var uri = cluster.NodesUris().First(); var nestIndex = "logs-from-nest"; Announce("NEST + Elastic.CommonSchema", nestIndex); var settings = new ConnectionSettings(uri).EnableDebugMode(); var client = new ElasticClient(settings); var bulkAll = client.BulkAll(GetEcsEvents(10_000), b => b .Index(nestIndex) .Size(1_000) .RefreshOnCompleted() .BufferToBulk((bulk, items) => bulk.CreateMany(items)) ); bulkAll.Wait(TimeSpan.FromMinutes(2), r => Console.WriteLine("Indexed 1000 events")); Check(client, nestIndex); // Using Serilog ElasticsearchSink var sinkIndex = "logs-from-sink"; Announce("Elastic.CommonSchema.Serilog", sinkIndex); var options = new ElasticsearchSinkOptions(uri) { CustomFormatter = new EcsTextFormatter(), IndexFormat = sinkIndex + "-{0:yyyy-MM-dd}", DetectElasticsearchVersion = true, BatchAction = ElasticOpType.Create, BatchPostingLimit = 1_000, }; var logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Elasticsearch(options) .CreateLogger(); Log.Logger = logger; foreach (var e in GetEcsEvents(10_000)) { Log.Debug("Reusing {field1} by {another_field}: {message}", "some value", true, e.Message); } Log.CloseAndFlush(); Check(client, $"{sinkIndex}-*"); }
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseSerilog((ctx, config) => { config.ReadFrom.Configuration(ctx.Configuration); var formatterConfig = new EcsTextFormatterConfiguration(); formatterConfig.MapHttpContext(ctx.Configuration.Get <HttpContextAccessor>()); var formatter = new EcsTextFormatter(formatterConfig); config.WriteTo.Console(formatter); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup <Startup>().UseUrls(Environment.GetEnvironmentVariable("BASE_URL") ?? "http://*:5111"); });
private static void TestLogger() { var formatterConfig = new EcsTextFormatterConfiguration(); var formatter = new EcsTextFormatter(formatterConfig); var loggerMS = Devmasters.Log.Logger.CreateLogger("adsf", Devmasters.Log.Logger.DefaultConfiguration() .MinimumLevel.Is(Serilog.Events.LogEventLevel.Debug) .Enrich.WithProperty("codeversion", System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString()) .AddFileLoggerFilePerLevel("c:/Data/Logs/pl/", "slog.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {SourceContext} [{Level:u3}] {Message:lj}{NewLine}{Exception}", rollingInterval: RollingInterval.Day, fileSizeLimitBytes: null, retainedFileCountLimit: 9) ); loggerMS.Debug("test debug"); loggerMS.Info("test info"); loggerMS.Warning("test warn"); loggerMS.Error("test err"); loggerMS.CloseAndFlush(); var logger = Devmasters.Log.Logger.CreateLogger("DevPlayground", Devmasters.Log.Logger.DefaultConfiguration() .Enrich.WithProperty("codeversion", System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString()) ); var log = new Devmasters.Log.LogMessage() .SetMessage("asdf-info-adv2") .SetException(new ArgumentNullException("param", "message about exc")) .SetCustomKeyValue("simpleKey", "simplevalue") .SetCustomKeyValue("advKey", new { action = "akce2", subject = "zdroj" }) ; logger.Info(log); var log2 = new Devmasters.Log.LogMessage() .SetMessage("asdf-warn-adv2") .SetException(new ArgumentNullException("param", "message about exc")) .SetCustomKeyValue("simpleKey", "simplevalue") .SetCustomKeyValue("advKey", new { action = "akce3", subject = "zdroj3" }) .SetContext(Devmasters.Log.CallingMethodDetail.FullStack) ; logger.Warning(log2); }
public LogEventPropFilterTests(ITestOutputHelper output) : base(output) { LoggerConfiguration = LoggerConfiguration .Enrich.WithThreadId() .Enrich.WithThreadName() .Enrich.WithMachineName() .Enrich.WithProcessId() .Enrich.WithProcessName() .Enrich.WithEnvironmentUserName() .Enrich.WithElasticApmCorrelationInfo(); Formatter = new EcsTextFormatter(new EcsTextFormatterConfiguration() .LogEventPropertiesToFilter(new HashSet <string>() { { "foo" } })); }
public void NullFilterLogEventProperty() => TestLogger((logger, getLogEvents) => { Formatter = new EcsTextFormatter(new EcsTextFormatterConfiguration() .LogEventPropertiesToFilter(null)); var evnt = BuildLogEvent(); logger.Write(evnt); var logEvents = getLogEvents(); logEvents.Should().HaveCount(1); var ecsEvents = ToEcsEvents(logEvents); var(_, info) = ecsEvents.First(); info.Log.Level.Should().Be("Information"); info.Error.Should().BeNull(); info.Metadata.Should().Contain("bar", "bbb"); info.Metadata.Should().Contain("foo", "aaa"); });
public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup <Startup>() .UseSerilog((ctx, config) => { config.ReadFrom.Configuration(ctx.Configuration); // Ensure HttpContextAccessor is accessible var httpAccessor = ctx.Configuration.Get <HttpContextAccessor>(); // Create a formatter configuration to se this accessor var formatterConfig = new EcsTextFormatterConfiguration(); formatterConfig.MapHttpContext(httpAccessor); // Write events to the console using this configration var formatter = new EcsTextFormatter(formatterConfig); config.WriteTo.Console(formatter); }) .UseKestrel() .Build();
public void CaseSensitiveFilterLogEventProperty() => TestLogger((logger, getLogEvents) => { Formatter = new EcsTextFormatter(new EcsTextFormatterConfiguration() .LogEventPropertiesToFilter(new HashSet <string>(StringComparer.Ordinal) { { "FOO" } })); var evnt = BuildLogEvent(); logger.Write(evnt); var logEvents = getLogEvents(); logEvents.Should().HaveCount(1); var ecsEvents = ToEcsEvents(logEvents); var(_, info) = ecsEvents.First(); info.Log.Level.Should().Be("Information"); info.Error.Should().BeNull(); info.Metadata.Should().Contain("bar", "bbb"); info.Metadata.Should().Contain("foo", "aaa"); });