Skip to content

Upgrades to Serilog 4.0 #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
using Google.Cloud.Logging.Type;
using Google.Cloud.Logging.V2;
using Google.Protobuf.WellKnownTypes;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.PeriodicBatching;

namespace Serilog.Sinks.GoogleCloudLogging;

Expand Down Expand Up @@ -62,7 +62,7 @@ public GoogleCloudLoggingSink(GoogleCloudLoggingSinkOptions sinkOptions, ITextFo
: new LoggingServiceV2ClientBuilder { JsonCredentials = _sinkOptions.GoogleCredentialJson }.Build();
}

public Task EmitBatchAsync(IEnumerable<LogEvent> events)
public Task EmitBatchAsync(IReadOnlyCollection<LogEvent> events)
{
using var writer = new StringWriter();
var entries = new List<LogEntry>();
Expand Down Expand Up @@ -104,6 +104,18 @@ private LogEntry CreateLogEntry(LogEvent evnt, StringWriter writer)
HandleSpecialProperty(log, property.Key, property.Value);
}

if (_sinkOptions.UseLogCorrelation)
{
if (evnt.TraceId.ToString() is { Length: > 0 } traceId)
{
log.Trace = $"projects/{_projectId}/traces/{traceId}";
}
if (evnt.SpanId?.ToString() is { Length: > 0 } spanId)
{
log.SpanId = spanId;
}
}

if (_serviceContext != null)
jsonPayload.Fields.Add("serviceContext", Value.ForStruct(_serviceContext));

Expand All @@ -119,12 +131,6 @@ private void HandleSpecialProperty(LogEntry log, string key, LogEventPropertyVal

if (_sinkOptions.UseLogCorrelation)
{
if (key.Equals("TraceId", StringComparison.OrdinalIgnoreCase))
log.Trace = $"projects/{_projectId}/traces/{GetString(value)}";

if (key.Equals("SpanId", StringComparison.OrdinalIgnoreCase))
log.SpanId = GetString(value);

if (key.Equals("TraceSampled", StringComparison.OrdinalIgnoreCase))
log.TraceSampled = GetBoolean(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Formatting.Display;
using Serilog.Sinks.PeriodicBatching;

namespace Serilog.Sinks.GoogleCloudLogging;

Expand Down Expand Up @@ -39,17 +38,16 @@ public static LoggerConfiguration GoogleCloudLogging(
// formatter can be null if neither parameters are provided
textFormatter ??= !String.IsNullOrWhiteSpace(outputTemplate) ? new MessageTemplateTextFormatter(outputTemplate) : null;

var batchingOptions = new PeriodicBatchingSinkOptions
var batchingOptions = new BatchingOptions
{
BatchSizeLimit = batchSizeLimit ?? 100,
Period = period ?? TimeSpan.FromSeconds(5),
BufferingTimeLimit = period ?? TimeSpan.FromSeconds(5),
QueueLimit = queueLimit
};

var sink = new GoogleCloudLoggingSink(sinkOptions, textFormatter);
var batchingSink = new PeriodicBatchingSink(sink, batchingOptions);

return loggerConfiguration.Sink(batchingSink, restrictedToMinimumLevel, levelSwitch);
return loggerConfiguration.Sink(sink, batchingOptions, restrictedToMinimumLevel, levelSwitch);
}

/// <summary>
Expand Down
15 changes: 11 additions & 4 deletions src/Serilog.Sinks.GoogleCloudLogging/LogFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@

namespace Serilog.Sinks.GoogleCloudLogging;

internal class LogFormatter
internal partial class LogFormatter
{
private readonly ITextFormatter? _textFormatter;

private static readonly Dictionary<string, string> LogNameCache = new(StringComparer.Ordinal);
private static readonly Regex LogNameUnsafeChars = new("[^0-9A-Z._/-]+", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant);

#if NET8_0_OR_GREATER
[GeneratedRegex("[^0-9A-Z._/-]+", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant)]
private static partial Regex LogNameUnsafeChars();
#else
private static readonly Regex LogNameUnsafeCharsRegex = new("[^0-9A-Z._/-]+", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant);
private static Regex LogNameUnsafeChars() => LogNameUnsafeCharsRegex;
#endif

public LogFormatter(ITextFormatter? textFormatter)
{
Expand Down Expand Up @@ -114,9 +121,9 @@ public static string CreateLogName(string projectId, string name)
{
// name must only contain: letters, numbers, underscore, hyphen, forward slash, period
// limited to 512 characters and must be url-encoded (using 500 char limit here to be safe)
var safeChars = LogNameUnsafeChars.Replace(name, "");
var safeChars = LogNameUnsafeChars().Replace(name, "");
var truncated = safeChars.Length > 500 ? safeChars.Substring(0, 500) : safeChars;
var encoded = UrlEncoder.Default.Encode(safeChars);
var encoded = UrlEncoder.Default.Encode(truncated);

// LogName class creates templated string matching GCP requirements
logName = new LogName(projectId, encoded).ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<TargetFrameworks>net6.0;net5.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net8.0;net6.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand All @@ -29,14 +29,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Google.Cloud.Logging.V2" Version="4.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PackageReference Include="Google.Cloud.Logging.V2" Version="4.4.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.3.1" />
<PackageReference Include="System.Text.Encodings.Web" Version="6.0.0" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Condition="'$(TargetFramework)' == 'net6.0'" Include="System.Text.Encodings.Web" Version="6.0.0" />
<PackageReference Condition="'$(TargetFramework)' != 'net6.0'" Include="System.Text.Encodings.Web" Version="8.0.0" />
</ItemGroup>

</Project>
8 changes: 4 additions & 4 deletions src/TestWeb/TestWeb.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>default</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down