Skip to content

Commit

Permalink
Merge pull request #7 from olvesh/main
Browse files Browse the repository at this point in the history
Added quotes handling on values and msg
  • Loading branch information
eiximenis authored May 25, 2023
2 parents 2400ce7 + c74fd35 commit e157ce6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions Serilog.Logfmt/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
<PackageProjectUrl>https://github.com/eiximenis/Serilog.Logfmt/</PackageProjectUrl>
<RepositoryUrl>https://github.com/eiximenis/Serilog.Logfmt</RepositoryUrl>
<Package>Serilog.Logfmt</Package>
<Version>1.0.3$(VersionSuffix)</Version>
<Version>1.0.4$(VersionSuffix)</Version>
<Authors>Eiximenis</Authors>
<Company>Lo Crestià</Company>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<!-- Enable deterministic build on GH Actions -->
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
</Project>
</Project>
8 changes: 4 additions & 4 deletions Serilog.Logfmt/LogfmtFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public void Format(LogEvent logEvent, TextWriter output)
msg = sw.ToLogfmtQuotedString(_options.DoubleQuotesAction);
}

if (msg.Contains(" "))
if (msg.Contains(" ") || msg.Contains("="))
{
output.WriteLine($@"""{msg}""");
}
else
{
output.WriteLine(msg);
}

if (logEvent.Exception != null)
{
LogException(logEvent, output);
Expand All @@ -73,7 +73,7 @@ private void LogException(LogEvent logEvent, TextWriter output)
output.Write("ts={0} ", logEvent.Timestamp.UtcDateTime.ToString("o"));
if (dataOptions != LogfmtExceptionDataFormat.None)
{

if (dataOptions.HasFlag(LogfmtExceptionDataFormat.Level))
{
output.Write("level={0} ", _options.GrafanaLevels ? "err" : LogEventLevel.Error.ToString());
Expand Down Expand Up @@ -112,7 +112,7 @@ private string GrafanaLevelValue(LogEventLevel level)
case LogEventLevel.Verbose: return "trace";
default: return "unknown";
}
}
}

private string GetNormalizedKeyCase(string key)
{
Expand Down
2 changes: 1 addition & 1 deletion Serilog.Logfmt/LogfmtValueFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override bool VisitScalarValue(TextWriter state, ScalarValue scalar)
var keyName = GetFullKeyName();
var svalue = scalar.Value?.ToString() ?? "\"\"";
state.Write("{0}=", keyName);
var needQuotes = svalue.Contains(" ");
var needQuotes = svalue.Contains(" ") || svalue.Contains("=");
if (needQuotes)
{
switch (_options.DoubleQuotesAction)
Expand Down
3 changes: 3 additions & 0 deletions TestApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
var logger = lf.CreateLogger("Startup");
LogContext.PushProperty("complex", new { Name = @"Property ""DOUBLE QUOTES"" on it", When = DateTime.UtcNow, Value = 42, Sub = new { Name = "Test", Iv = 32 } }, true);
LogContext.PushProperty("str", "Simple string property");
LogContext.PushProperty("equals", "this=that");
LogContext.PushProperty("int", 42);
LogContext.PushProperty("test", new[] {10, 100, 1000});
var value = @"This value also have ""double quotes"" on it";
logger.LogInformation(@"Message with ""double quotes"" and a str value: {value} :) ", value);
logger.LogInformation("justalog");
logger.LogInformation("justalog=with=equals");
await context.Response.WriteAsync("Hello World!");
});
Expand Down
2 changes: 1 addition & 1 deletion TestApi/Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<UserSecretsId>13c18871-4940-4814-8884-4441168b3cea</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
Expand Down

0 comments on commit e157ce6

Please sign in to comment.