Skip to content

Commit

Permalink
Add MapCustom to Extensions logging (#448)
Browse files Browse the repository at this point in the history
This existed for Serilog but not Extensions Logging.
  • Loading branch information
Mpdreamz authored Sep 24, 2024
1 parent 4108bf9 commit b77551f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
// See https://aka.ms/new-console-template for more information

using Elastic.CommonSchema;
using Elastic.Extensions.Logging.Console;
using Elastic.Extensions.Logging.Console.Example;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Host = Microsoft.Extensions.Hosting.Host;

await Host.CreateDefaultBuilder(args)
.UseConsoleLifetime()
.ConfigureAppConfiguration((_, configurationBuilder) =>
{
configurationBuilder.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
})
.ConfigureLogging((_, loggingBuilder) => loggingBuilder.AddEcsConsole())
.ConfigureLogging((_, loggingBuilder) => loggingBuilder.AddEcsConsole(configure: c =>
{
c.MapCustom = l => l.Organization = new Organization { Name = "my-organization" };
}))
.ConfigureServices((_, services) =>
{
services.AddHostedService<ExampleService>();
Expand Down
10 changes: 10 additions & 0 deletions src/Elastic.Extensions.Logging.Common/ILogEventCreationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ public interface ILogEventCreationOptions : IEcsDocumentCreationOptions
/// </summary>
string ListSeparator { get; set; }
}

/// <inheritdoc cref="ILogEventCreationOptions"/>
public interface ILogEventCreationOptions<TEcsDocument> : ILogEventCreationOptions
where TEcsDocument : EcsDocument, new()
{
/// <summary>
/// Allows you to enrich <typeparamref name="TEcsDocument"/> using <see cref="LogEvent"/> before its being formatted
/// </summary>
Action<TEcsDocument>? MapCustom { get; set; }
}
6 changes: 4 additions & 2 deletions src/Elastic.Extensions.Logging.Console/EcsConsoleFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeP
var logLevel = logEntry.LogLevel;
var categoryName = logEntry.Category;
var eventId = logEntry.EventId;

logEvent.Message = message;
logEvent.Log = new Log { Level = logLevel.ToEcsLogLevelString(), Logger = categoryName };
logEvent.Event = new Event { Action = eventId.Name, Code = eventId.Id.ToString(), Severity = logLevel.ToEcsSeverity() };
logEvent.Message = message;

logEvent.Agent = DefaultAgent;

if (_options.Tags is { Length: > 0 }) logEvent.Tags = _options.Tags;
Expand All @@ -48,6 +48,8 @@ public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeP
// These will overwrite any scope values with the same name
logEvent.AddStateValues(logEntry.State, _options);

_options.MapCustom?.Invoke(logEvent);

textWriter.WriteLine(logEvent.Serialize());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Elastic.Extensions.Logging.Console;

/// <summary> </summary>
public class EcsConsoleFormatterOptions : ConsoleFormatterOptions, ILogEventCreationOptions
public class EcsConsoleFormatterOptions : ConsoleFormatterOptions, ILogEventCreationOptions<LogEvent>
{
/// <inheritdoc cref="IEcsDocumentCreationOptions.IncludeHost"/>
public bool IncludeHost { get; set; } = true;
Expand All @@ -25,4 +25,7 @@ public class EcsConsoleFormatterOptions : ConsoleFormatterOptions, ILogEventCrea

/// <inheritdoc cref="ILogEventCreationOptions.ListSeparator"/>
public string ListSeparator { get; set; } = ", ";

/// <inheritdoc cref="ILogEventCreationOptions{TEcsDocument}.MapCustom"/>
public Action<LogEvent>? MapCustom { get; set; }
}
2 changes: 2 additions & 0 deletions src/Elastic.Extensions.Logging/ElasticsearchLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ Func<TState, Exception, string> formatter
// These will overwrite any scope values with the same name
logEvent.AddStateValues(state, _options);

_options.MapCustom?.Invoke(logEvent);

return logEvent;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Elastic.CommonSchema;
using Elastic.Extensions.Logging.Common;
using Elastic.Ingest.Elasticsearch;
Expand All @@ -8,7 +9,7 @@ namespace Elastic.Extensions.Logging.Options
/// <summary>
/// Provide options to <see cref="ElasticsearchLogger"/> to control how data gets written to Elasticsearch
/// </summary>
public class ElasticsearchLoggerOptions : ILogEventCreationOptions
public class ElasticsearchLoggerOptions : ILogEventCreationOptions<LogEvent>
{
/// <summary>
/// Gets or sets a flag indicating whether host details should be included in the message. Defaults to <c>true</c>.
Expand Down Expand Up @@ -91,5 +92,8 @@ public class ElasticsearchLoggerOptions : ILogEventCreationOptions
/// <para>If set takes precedence over <see cref="ShipTo"/> </para>
/// </summary>
public ITransport? Transport { get; set; }

/// <inheritdoc cref="ILogEventCreationOptions{TEcsDocument}.MapCustom"/>
public Action<LogEvent>? MapCustom { get; set; }
}
}

0 comments on commit b77551f

Please sign in to comment.