Skip to content

Commit

Permalink
Packages update, added http sink
Browse files Browse the repository at this point in the history
  • Loading branch information
mrapavy committed Feb 5, 2024
1 parent fe3c4a7 commit b1e52f9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using System.Runtime.InteropServices;
using Microsoft.Extensions.Hosting;
using Serilog;

namespace SerilogEcsLogging.Logging;

public static class HostBuilderExtensions
{
public static string DefaultLogDirectory => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? $"{Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData, Environment.SpecialFolderOption.Create)}/EFTLog" : "/var/log";

public static bool RunningInContainer => string.Equals(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER"), "true", StringComparison.InvariantCultureIgnoreCase);

public static IHostBuilder UseSerilogEvents(this IHostBuilder builder, Action<HostBuilderContext, LoggerConfiguration>? configureLogger = null, bool logEcsEvents = true, bool logToConsole = true, bool consoleToStdErr = false, string? logFilePath = null)
{
return builder.UseSerilog((context, configuration) => {
Expand Down
25 changes: 19 additions & 6 deletions src/SerilogEcsLogging/Logging/LoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Elastic.CommonSchema.Serilog;
using System.Reflection;
using Elastic.CommonSchema.Serilog;
using Serilog;
using Serilog.Events;

Expand All @@ -8,15 +9,15 @@ public static class LoggerConfigurationExtensions
{
public const string TraceTemplate = "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}][{MachineName}][{Level:u3}][{SourceContext}][{ThreadId}]{Scope} {Message}{NewLine}{Exception}";

public static EcsTextFormatter CreateEcsTextFormatter() => new EcsTextFormatter(new EcsTextFormatterConfiguration {
public static EcsTextFormatter EcsTextFormatter => new EcsTextFormatter(new EcsTextFormatterConfiguration {
IncludeHost = true,
IncludeProcess = true,
IncludeUser = true,
MapCustom = EcsMapper.MapLogEvent,
LogEventPropertiesToFilter = new HashSet<string> {"metadata.*", "labels.*"}
});

public static LoggerConfiguration ConfigureEcs(this LoggerConfiguration configuration, bool logEcsEvents = true, bool logToConsole = true, bool consoleToStdErr = false, string? logFilePath = null)
public static LoggerConfiguration ConfigureEcs(this LoggerConfiguration configuration, bool logEcsEvents = true, bool logToConsole = true, bool consoleToStdErr = false, string? logFilePath = null, string? httpEndpoint = null, TimeSpan? httpPostPeriod = null, int? logEventsInHttpBatchLimit = 1000)
{
configuration
.Enrich.FromLogContext()
Expand All @@ -29,14 +30,14 @@ public static LoggerConfiguration ConfigureEcs(this LoggerConfiguration configur
.Enrich.WithCorrelationId()
.Enrich.WithAssemblyName()
.Enrich.WithAssemblyVersion();

if (logToConsole)
{
LogEventLevel? stdErrFromLevel = consoleToStdErr ? LogEventLevel.Verbose : null;
configuration.WriteTo.Async(c => {
if (logEcsEvents)
{
c.Console(CreateEcsTextFormatter(), standardErrorFromLevel: stdErrFromLevel);
c.Console(EcsTextFormatter, standardErrorFromLevel: stdErrFromLevel);
}
else
{
Expand All @@ -54,14 +55,26 @@ public static LoggerConfiguration ConfigureEcs(this LoggerConfiguration configur
var extension = Path.GetExtension(logFilePath);
logFilePath = Path.Combine(directory, $"{filename}.ECS.{extension}");

configuration.WriteTo.Async(c => c.File(CreateEcsTextFormatter(), logFilePath, rollingInterval: RollingInterval.Day));
configuration.WriteTo.Async(c => c.File(EcsTextFormatter, logFilePath, rollingInterval: RollingInterval.Day));
}
else
{
configuration.WriteTo.Async(c => c.File(logFilePath, rollingInterval: RollingInterval.Day, outputTemplate: TraceTemplate));
}
}

if (!string.IsNullOrEmpty(httpEndpoint))
{
var path = logFilePath;
if (string.IsNullOrEmpty(path))
{
var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly();
path = Path.Combine(HostBuilderExtensions.DefaultLogDirectory, assembly.GetName().Name ?? Guid.NewGuid().ToString("N"));
}

configuration.WriteTo.DurableHttpUsingFileSizeRolledBuffers(httpEndpoint, $"{path}-buffer", textFormatter: logEcsEvents ? EcsTextFormatter : null, period: httpPostPeriod, logEventsInBatchLimit: logEventsInHttpBatchLimit);
}

return configuration;
}
}
15 changes: 8 additions & 7 deletions src/SerilogEcsLogging/SerilogEcsLogging.csproj
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>mrapavy</Authors>
<PackageProjectUrl>https://github.com/mrapavy/serilog-ecs-logging</PackageProjectUrl>
<Version>1.1.6</Version>
<Version>1.1.7</Version>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Elastic.CommonSchema.Serilog" Version="8.6.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Serilog.Enrichers.AssemblyName" Version="1.0.9" />
<PackageReference Include="Serilog.Enrichers.CorrelationId" Version="3.0.1" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.3.0" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Http" Version="8.0.0" />
</ItemGroup>

</Project>

0 comments on commit b1e52f9

Please sign in to comment.