From b76c07413fc4fccd87de1c841776d89b9f710ef0 Mon Sep 17 00:00:00 2001 From: Sophie Rudd Date: Mon, 23 Sep 2024 00:01:22 +0100 Subject: [PATCH 1/3] Added ability to configure Logfmt formatter in configuration file --- .../ConfigurationTesting.csproj | 36 ++++++++++ ConfigurationTesting/UnitTest1.cs | 67 +++++++++++++++++++ .../resources/complex_separators.json | 17 +++++ .../preserved_case_and_log_levels.json | 17 +++++ .../resources/tmp/testlog.txt | 0 .../resources/tmp/testlog_complex.txt | 5 ++ .../resources/tmp/testlog_preserved.txt | 5 ++ Serilog.Logfmt.sln | 14 ++++ Serilog.Logfmt/LogfmtFormatter.cs | 61 +++++++++++++++++ Serilog.Logfmt/Serilog.Logfmt.csproj | 2 +- 10 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 ConfigurationTesting/ConfigurationTesting.csproj create mode 100644 ConfigurationTesting/UnitTest1.cs create mode 100644 ConfigurationTesting/resources/complex_separators.json create mode 100644 ConfigurationTesting/resources/preserved_case_and_log_levels.json create mode 100644 ConfigurationTesting/resources/tmp/testlog.txt create mode 100644 ConfigurationTesting/resources/tmp/testlog_complex.txt create mode 100644 ConfigurationTesting/resources/tmp/testlog_preserved.txt diff --git a/ConfigurationTesting/ConfigurationTesting.csproj b/ConfigurationTesting/ConfigurationTesting.csproj new file mode 100644 index 0000000..9ef8543 --- /dev/null +++ b/ConfigurationTesting/ConfigurationTesting.csproj @@ -0,0 +1,36 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ConfigurationTesting/UnitTest1.cs b/ConfigurationTesting/UnitTest1.cs new file mode 100644 index 0000000..f94edb6 --- /dev/null +++ b/ConfigurationTesting/UnitTest1.cs @@ -0,0 +1,67 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration.Json; +using Microsoft.Extensions.FileProviders; +using Serilog; +using Serilog.Logfmt; +using Serilog.Context; +using Xunit.Abstractions; + +namespace ConfigurationTesting; + +public class UnitTest1 : IDisposable +{ + + private readonly ITestOutputHelper output; + public UnitTest1(ITestOutputHelper output) + { + this.output = output; + } + [Fact] + public void PreservedCaseAndLogLevels() + { + JsonConfigurationSource jsonConfig = new JsonConfigurationSource(); + jsonConfig.Path = "\\resources\\preserved_case_and_log_levels.json"; + jsonConfig.FileProvider = new PhysicalFileProvider(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName); + var configuration = new ConfigurationBuilder() + .Add(jsonConfig) + .Build(); + + var logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .CreateLogger(); + + logger.Warning("this is a test!"); + + Assert.False(false); + } + + [Fact] + public void ComplexSeparators() + { + JsonConfigurationSource jsonConfig = new JsonConfigurationSource(); + jsonConfig.Path = "\\resources\\complex_separators.json"; + jsonConfig.FileProvider = new PhysicalFileProvider(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName); + var configuration = new ConfigurationBuilder() + .Add(jsonConfig) + .Build(); + + var logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .Enrich.FromLogContext() + .CreateLogger(); + + LogContext.PushProperty("complex", new {A = "alpha", B = "beta"}, true); + + logger.Error("this is a test!"); + + Assert.False(false); + } + + public void Dispose() + { + foreach (var file in Directory.GetFiles(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName + "\\resources\\tmp")) + { + Directory.Delete(file); + } + } +} \ No newline at end of file diff --git a/ConfigurationTesting/resources/complex_separators.json b/ConfigurationTesting/resources/complex_separators.json new file mode 100644 index 0000000..e590cff --- /dev/null +++ b/ConfigurationTesting/resources/complex_separators.json @@ -0,0 +1,17 @@ +{ + "Serilog": { + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "..\\..\\..\\resources\\tmp\\testlog_complex.txt", + "formatter": { + "type": "Serilog.Logfmt.LogfmtFormatter, Serilog.Logfmt", + "IncludeAllProperties": "true", + "ComplexPropertySeparator": ":" + } + } + } + ] + } +} \ No newline at end of file diff --git a/ConfigurationTesting/resources/preserved_case_and_log_levels.json b/ConfigurationTesting/resources/preserved_case_and_log_levels.json new file mode 100644 index 0000000..5f6b67e --- /dev/null +++ b/ConfigurationTesting/resources/preserved_case_and_log_levels.json @@ -0,0 +1,17 @@ +{ + "Serilog": { + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "..\\..\\..\\resources\\tmp\\testlog_preserved.txt", + "formatter": { + "type": "Serilog.Logfmt.LogfmtFormatter, Serilog.Logfmt", + "PreserveCase": "true", + "PreserveSerilogLogLevels": "true" + } + } + } + ] + } +} \ No newline at end of file diff --git a/ConfigurationTesting/resources/tmp/testlog.txt b/ConfigurationTesting/resources/tmp/testlog.txt new file mode 100644 index 0000000..e69de29 diff --git a/ConfigurationTesting/resources/tmp/testlog_complex.txt b/ConfigurationTesting/resources/tmp/testlog_complex.txt new file mode 100644 index 0000000..b56b33d --- /dev/null +++ b/ConfigurationTesting/resources/tmp/testlog_complex.txt @@ -0,0 +1,5 @@ +ts=2024-09-22T22:46:31.8790575Z level=err msg="this is a test!" +ts=2024-09-22T22:47:22.1503711Z level=err complex="{ A = alpha, B = beta }" msg="this is a test!" +ts=2024-09-22T22:48:14.0440113Z level=err complex.name="Property DOUBLE QUOTES on it" complex.when="22/09/2024 22:48:14" complex.value=42 complex.sub.name=Test complex.sub.iv=32 msg="this is a test!" +ts=2024-09-22T22:48:46.1693296Z level=err complex.a=alpha complex.b=beta msg="this is a test!" +ts=2024-09-22T22:49:16.9228369Z level=err complex:a=alpha complex:b=beta msg="this is a test!" diff --git a/ConfigurationTesting/resources/tmp/testlog_preserved.txt b/ConfigurationTesting/resources/tmp/testlog_preserved.txt new file mode 100644 index 0000000..0ffe5f9 --- /dev/null +++ b/ConfigurationTesting/resources/tmp/testlog_preserved.txt @@ -0,0 +1,5 @@ +ts=2024-09-22T22:46:31.8648941Z level=Warning msg="this is a test!" +ts=2024-09-22T22:47:22.1338149Z level=Warning msg="this is a test!" +ts=2024-09-22T22:48:14.0272200Z level=Warning msg="this is a test!" +ts=2024-09-22T22:48:46.1494234Z level=Warning msg="this is a test!" +ts=2024-09-22T22:49:16.8977256Z level=Warning msg="this is a test!" diff --git a/Serilog.Logfmt.sln b/Serilog.Logfmt.sln index d31d138..7fdfe2a 100644 --- a/Serilog.Logfmt.sln +++ b/Serilog.Logfmt.sln @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Logfmt", "Serilog.L EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "TestApi\Test.csproj", "{D0917732-847C-43E6-ACEE-B5D6636599B2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfigurationTesting", "ConfigurationTesting\ConfigurationTesting.csproj", "{5C012B3F-640B-42C6-9F84-BAEB500B9755}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -41,6 +43,18 @@ Global {D0917732-847C-43E6-ACEE-B5D6636599B2}.Release|x64.Build.0 = Release|Any CPU {D0917732-847C-43E6-ACEE-B5D6636599B2}.Release|x86.ActiveCfg = Release|Any CPU {D0917732-847C-43E6-ACEE-B5D6636599B2}.Release|x86.Build.0 = Release|Any CPU + {5C012B3F-640B-42C6-9F84-BAEB500B9755}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5C012B3F-640B-42C6-9F84-BAEB500B9755}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5C012B3F-640B-42C6-9F84-BAEB500B9755}.Debug|x64.ActiveCfg = Debug|Any CPU + {5C012B3F-640B-42C6-9F84-BAEB500B9755}.Debug|x64.Build.0 = Debug|Any CPU + {5C012B3F-640B-42C6-9F84-BAEB500B9755}.Debug|x86.ActiveCfg = Debug|Any CPU + {5C012B3F-640B-42C6-9F84-BAEB500B9755}.Debug|x86.Build.0 = Debug|Any CPU + {5C012B3F-640B-42C6-9F84-BAEB500B9755}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5C012B3F-640B-42C6-9F84-BAEB500B9755}.Release|Any CPU.Build.0 = Release|Any CPU + {5C012B3F-640B-42C6-9F84-BAEB500B9755}.Release|x64.ActiveCfg = Release|Any CPU + {5C012B3F-640B-42C6-9F84-BAEB500B9755}.Release|x64.Build.0 = Release|Any CPU + {5C012B3F-640B-42C6-9F84-BAEB500B9755}.Release|x86.ActiveCfg = Release|Any CPU + {5C012B3F-640B-42C6-9F84-BAEB500B9755}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Serilog.Logfmt/LogfmtFormatter.cs b/Serilog.Logfmt/LogfmtFormatter.cs index 6224eab..1954617 100644 --- a/Serilog.Logfmt/LogfmtFormatter.cs +++ b/Serilog.Logfmt/LogfmtFormatter.cs @@ -25,6 +25,67 @@ public LogfmtFormatter(Action configOptions = null) _propertyKeyFilter = _options.PropertyKeyFilter; } + public LogfmtFormatter(bool PreserveCase = false, bool PreserveSerilogLogLevels = false, string ComplexPropertySeparator = ".", + bool IncludeAllProperties = false, string OnDoubleQuotes = "Remove", string StackTraceFormat = "None", + List ExceptionDataFormat = null) + { + if (ExceptionDataFormat == null) + { + ExceptionDataFormat = new List{"Message","Type","Level"}; + } + Action optionsAction = null; + if (PreserveCase) optionsAction += options => options.PreserveCase(); + if (PreserveSerilogLogLevels) optionsAction += options => options.PreserveSerilogLogLevels(); + if (IncludeAllProperties) optionsAction += options => options.IncludeAllProperties(); + + optionsAction += options => options.UseComplexPropertySeparator(ComplexPropertySeparator); + + switch (OnDoubleQuotes) + { + case "Escape": + optionsAction += options => options.OnDoubleQuotes(q => q.Escape()); + break; + case "ConvertToSingle": + optionsAction += options => options.OnDoubleQuotes(q => q.ConvertToSingle()); + break; + case "Preserve": + optionsAction += options => options.OnDoubleQuotes(q => q.Preserve()); + break; + default: + case "Remove": + optionsAction += options => options.OnDoubleQuotes(q => q.Remove()); + break; + } + + Action exceptionsOptionsAction = null; + + switch (StackTraceFormat) + { + case "All": + exceptionsOptionsAction += exceptionOptions => exceptionOptions.LogStackTrace(LogfmtStackTraceFormat.All); + break; + case "SingleLine": + exceptionsOptionsAction += exceptionOptions => exceptionOptions.LogStackTrace(LogfmtStackTraceFormat.SingleLine); + break; + default: + case "None": + exceptionsOptionsAction += exceptionOptions => exceptionOptions.LogStackTrace(LogfmtStackTraceFormat.None); + break; + } + + LogfmtExceptionDataFormat exceptionDataFlag = 0; + if (ExceptionDataFormat.Contains("Type")) exceptionDataFlag |= LogfmtExceptionDataFormat.Type; + if (ExceptionDataFormat.Contains("Message")) exceptionDataFlag |= LogfmtExceptionDataFormat.Message; + if (ExceptionDataFormat.Contains("Level")) exceptionDataFlag |= LogfmtExceptionDataFormat.Level; + exceptionsOptionsAction += exceptionOptions => exceptionOptions.LogExceptionData(exceptionDataFlag); + + optionsAction += options => options.OnException(exceptionsOptionsAction); + + _options = new LogfmtOptions(); + optionsAction?.Invoke(_options); + _propertyKeyFilter = _options.PropertyKeyFilter; + } + public void Format(LogEvent logEvent, TextWriter output) { if (logEvent == null) throw new ArgumentNullException(nameof(logEvent)); diff --git a/Serilog.Logfmt/Serilog.Logfmt.csproj b/Serilog.Logfmt/Serilog.Logfmt.csproj index eebfe31..51f8502 100644 --- a/Serilog.Logfmt/Serilog.Logfmt.csproj +++ b/Serilog.Logfmt/Serilog.Logfmt.csproj @@ -7,7 +7,7 @@ - + all runtime; build; native; contentfiles; analyzers From f82303fd67c846876450d0712d2a5a945a7a1229 Mon Sep 17 00:00:00 2001 From: Sophie Rudd Date: Mon, 23 Sep 2024 00:31:22 +0100 Subject: [PATCH 2/3] More testing --- .../ConfigurationTesting.csproj | 4 - ConfigurationTesting/ConfigurationTests.cs | 169 ++++++++++++++++++ ConfigurationTesting/UnitTest1.cs | 67 ------- .../resources/convert_double_quotes.json | 16 ++ .../resources/escape_double_quotes.json | 16 ++ .../resources/preserve_double_quotes.json | 16 ++ .../preserved_case_and_log_levels.json | 5 +- .../resources/remove_double_quotes.json | 16 ++ .../resources/tmp/testlog.txt | 0 .../resources/tmp/testlog_complex.txt | 5 - .../resources/tmp/testlog_preserved.txt | 5 - 11 files changed, 236 insertions(+), 83 deletions(-) create mode 100644 ConfigurationTesting/ConfigurationTests.cs delete mode 100644 ConfigurationTesting/UnitTest1.cs create mode 100644 ConfigurationTesting/resources/convert_double_quotes.json create mode 100644 ConfigurationTesting/resources/escape_double_quotes.json create mode 100644 ConfigurationTesting/resources/preserve_double_quotes.json create mode 100644 ConfigurationTesting/resources/remove_double_quotes.json delete mode 100644 ConfigurationTesting/resources/tmp/testlog.txt delete mode 100644 ConfigurationTesting/resources/tmp/testlog_complex.txt delete mode 100644 ConfigurationTesting/resources/tmp/testlog_preserved.txt diff --git a/ConfigurationTesting/ConfigurationTesting.csproj b/ConfigurationTesting/ConfigurationTesting.csproj index 9ef8543..a1e919f 100644 --- a/ConfigurationTesting/ConfigurationTesting.csproj +++ b/ConfigurationTesting/ConfigurationTesting.csproj @@ -29,8 +29,4 @@ - - - - diff --git a/ConfigurationTesting/ConfigurationTests.cs b/ConfigurationTesting/ConfigurationTests.cs new file mode 100644 index 0000000..175b1a6 --- /dev/null +++ b/ConfigurationTesting/ConfigurationTests.cs @@ -0,0 +1,169 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration.Json; +using Microsoft.Extensions.FileProviders; +using Serilog; +using Serilog.Logfmt; +using Serilog.Context; +using Xunit.Abstractions; +using Newtonsoft.Json.Linq; + +namespace ConfigurationTesting; + +public class ConfigurationTests : IDisposable +{ + + private readonly ITestOutputHelper output; + private readonly string basefolder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; + public ConfigurationTests(ITestOutputHelper output) + { + this.output = output; + } + [Fact] + public void PreservedCaseAndLogLevels() + { + JsonConfigurationSource jsonConfig = new JsonConfigurationSource(); + jsonConfig.Path = "\\resources\\preserved_case_and_log_levels.json"; + jsonConfig.FileProvider = new PhysicalFileProvider(basefolder); + var configuration = new ConfigurationBuilder() + .Add(jsonConfig) + .Build(); + + var logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .Enrich.FromLogContext() + .CreateLogger(); + + LogContext.PushProperty("InterestingProperty", "An interesting property"); + + logger.Warning("this is a test!"); + logger.Dispose(); + + var lineOfLog = File.ReadAllLines("..\\..\\..\\resources\\tmp\\testlog_preserved_case_and_log_levels.txt")[0]; + + Assert.Contains("level=Warning", lineOfLog); + Assert.Contains(@"InterestingProperty=""An interesting property""", lineOfLog); + } + + [Fact] + public void ComplexSeparators() + { + JsonConfigurationSource jsonConfig = new JsonConfigurationSource(); + jsonConfig.Path = "\\resources\\complex_separators.json"; + jsonConfig.FileProvider = new PhysicalFileProvider(basefolder); + var configuration = new ConfigurationBuilder() + .Add(jsonConfig) + .Build(); + + var logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .Enrich.FromLogContext() + .CreateLogger(); + + LogContext.PushProperty("complex", new {A = "alpha", B = "beta"}, true); + + logger.Error("this is a test!"); + logger.Dispose(); + + var lineOfLog = File.ReadAllLines("..\\..\\..\\resources\\tmp\\testlog_complex.txt")[0]; + + Assert.Contains("complex:a=alpha", lineOfLog); + Assert.Contains("complex:b=beta", lineOfLog); + } + + [Fact] + public void PreserveDoubles() + { + JsonConfigurationSource jsonConfig = new JsonConfigurationSource(); + jsonConfig.Path = "\\resources\\preserve_double_quotes.json"; + jsonConfig.FileProvider = new PhysicalFileProvider(basefolder); + var configuration = new ConfigurationBuilder() + .Add(jsonConfig) + .Build(); + + var logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .Enrich.FromLogContext() + .CreateLogger(); + + logger.Error(@"this is a ""double quotes"" test"); + logger.Dispose(); + + var lineOfLog = File.ReadAllLines("..\\..\\..\\resources\\tmp\\testlog_preserve_doubles.txt")[0]; + + Assert.Contains(@"msg=""this is a ""double quotes"" test""", lineOfLog); + } + + [Fact] + public void RemoveDoubles() + { + JsonConfigurationSource jsonConfig = new JsonConfigurationSource(); + jsonConfig.Path = "\\resources\\remove_double_quotes.json"; + jsonConfig.FileProvider = new PhysicalFileProvider(basefolder); + var configuration = new ConfigurationBuilder() + .Add(jsonConfig) + .Build(); + + var logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .Enrich.FromLogContext() + .CreateLogger(); + + logger.Error(@"this is a ""double quotes"" test"); + logger.Dispose(); + + var lineOfLog = File.ReadAllLines("..\\..\\..\\resources\\tmp\\testlog_remove_doubles.txt")[0]; + + Assert.Contains(@"msg=""this is a double quotes test""", lineOfLog); + } + + [Fact] + public void ConvertDoubles() + { + JsonConfigurationSource jsonConfig = new JsonConfigurationSource(); + jsonConfig.Path = "\\resources\\convert_double_quotes.json"; + jsonConfig.FileProvider = new PhysicalFileProvider(basefolder); + var configuration = new ConfigurationBuilder() + .Add(jsonConfig) + .Build(); + + var logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .Enrich.FromLogContext() + .CreateLogger(); + + logger.Error(@"this is a ""double quotes"" test"); + logger.Dispose(); + + var lineOfLog = File.ReadAllLines("..\\..\\..\\resources\\tmp\\testlog_convert_doubles.txt")[0]; + + Assert.Contains(@"msg=""this is a 'double quotes' test""", lineOfLog); + } + + [Fact] + public void EscapeDoubles() + { + JsonConfigurationSource jsonConfig = new JsonConfigurationSource(); + jsonConfig.Path = "\\resources\\escape_double_quotes.json"; + jsonConfig.FileProvider = new PhysicalFileProvider(basefolder); + var configuration = new ConfigurationBuilder() + .Add(jsonConfig) + .Build(); + + var logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .Enrich.FromLogContext() + .CreateLogger(); + + logger.Error(@"this is a ""double quotes"" test"); + logger.Dispose(); + + var lineOfLog = File.ReadAllLines("..\\..\\..\\resources\\tmp\\testlog_escape_doubles.txt")[0]; + + Assert.Contains(@"msg=""this is a \""double quotes\"" test""", lineOfLog); + } + + public void Dispose() + { + Directory.Delete(basefolder + "\\resources\\tmp"); + } +} \ No newline at end of file diff --git a/ConfigurationTesting/UnitTest1.cs b/ConfigurationTesting/UnitTest1.cs deleted file mode 100644 index f94edb6..0000000 --- a/ConfigurationTesting/UnitTest1.cs +++ /dev/null @@ -1,67 +0,0 @@ -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Configuration.Json; -using Microsoft.Extensions.FileProviders; -using Serilog; -using Serilog.Logfmt; -using Serilog.Context; -using Xunit.Abstractions; - -namespace ConfigurationTesting; - -public class UnitTest1 : IDisposable -{ - - private readonly ITestOutputHelper output; - public UnitTest1(ITestOutputHelper output) - { - this.output = output; - } - [Fact] - public void PreservedCaseAndLogLevels() - { - JsonConfigurationSource jsonConfig = new JsonConfigurationSource(); - jsonConfig.Path = "\\resources\\preserved_case_and_log_levels.json"; - jsonConfig.FileProvider = new PhysicalFileProvider(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName); - var configuration = new ConfigurationBuilder() - .Add(jsonConfig) - .Build(); - - var logger = new LoggerConfiguration() - .ReadFrom.Configuration(configuration) - .CreateLogger(); - - logger.Warning("this is a test!"); - - Assert.False(false); - } - - [Fact] - public void ComplexSeparators() - { - JsonConfigurationSource jsonConfig = new JsonConfigurationSource(); - jsonConfig.Path = "\\resources\\complex_separators.json"; - jsonConfig.FileProvider = new PhysicalFileProvider(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName); - var configuration = new ConfigurationBuilder() - .Add(jsonConfig) - .Build(); - - var logger = new LoggerConfiguration() - .ReadFrom.Configuration(configuration) - .Enrich.FromLogContext() - .CreateLogger(); - - LogContext.PushProperty("complex", new {A = "alpha", B = "beta"}, true); - - logger.Error("this is a test!"); - - Assert.False(false); - } - - public void Dispose() - { - foreach (var file in Directory.GetFiles(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName + "\\resources\\tmp")) - { - Directory.Delete(file); - } - } -} \ No newline at end of file diff --git a/ConfigurationTesting/resources/convert_double_quotes.json b/ConfigurationTesting/resources/convert_double_quotes.json new file mode 100644 index 0000000..59d600c --- /dev/null +++ b/ConfigurationTesting/resources/convert_double_quotes.json @@ -0,0 +1,16 @@ +{ + "Serilog": { + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "..\\..\\..\\resources\\tmp\\testlog_convert_doubles.txt", + "formatter": { + "type": "Serilog.Logfmt.LogfmtFormatter, Serilog.Logfmt", + "OnDoubleQuotes": "ConvertToSingle" + } + } + } + ] + } +} \ No newline at end of file diff --git a/ConfigurationTesting/resources/escape_double_quotes.json b/ConfigurationTesting/resources/escape_double_quotes.json new file mode 100644 index 0000000..d73a2cf --- /dev/null +++ b/ConfigurationTesting/resources/escape_double_quotes.json @@ -0,0 +1,16 @@ +{ + "Serilog": { + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "..\\..\\..\\resources\\tmp\\testlog_escape_doubles.txt", + "formatter": { + "type": "Serilog.Logfmt.LogfmtFormatter, Serilog.Logfmt", + "OnDoubleQuotes": "Escape" + } + } + } + ] + } +} \ No newline at end of file diff --git a/ConfigurationTesting/resources/preserve_double_quotes.json b/ConfigurationTesting/resources/preserve_double_quotes.json new file mode 100644 index 0000000..d1e1c79 --- /dev/null +++ b/ConfigurationTesting/resources/preserve_double_quotes.json @@ -0,0 +1,16 @@ +{ + "Serilog": { + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "..\\..\\..\\resources\\tmp\\testlog_preserve_doubles.txt", + "formatter": { + "type": "Serilog.Logfmt.LogfmtFormatter, Serilog.Logfmt", + "OnDoubleQuotes": "Preserve" + } + } + } + ] + } +} \ No newline at end of file diff --git a/ConfigurationTesting/resources/preserved_case_and_log_levels.json b/ConfigurationTesting/resources/preserved_case_and_log_levels.json index 5f6b67e..d2bfd6a 100644 --- a/ConfigurationTesting/resources/preserved_case_and_log_levels.json +++ b/ConfigurationTesting/resources/preserved_case_and_log_levels.json @@ -4,11 +4,12 @@ { "Name": "File", "Args": { - "path": "..\\..\\..\\resources\\tmp\\testlog_preserved.txt", + "path": "..\\..\\..\\resources\\tmp\\testlog_preserved_case_and_log_levels.txt", "formatter": { "type": "Serilog.Logfmt.LogfmtFormatter, Serilog.Logfmt", "PreserveCase": "true", - "PreserveSerilogLogLevels": "true" + "PreserveSerilogLogLevels": "true", + "IncludeAllProperties": "true" } } } diff --git a/ConfigurationTesting/resources/remove_double_quotes.json b/ConfigurationTesting/resources/remove_double_quotes.json new file mode 100644 index 0000000..e6d78f6 --- /dev/null +++ b/ConfigurationTesting/resources/remove_double_quotes.json @@ -0,0 +1,16 @@ +{ + "Serilog": { + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "..\\..\\..\\resources\\tmp\\testlog_remove_doubles.txt", + "formatter": { + "type": "Serilog.Logfmt.LogfmtFormatter, Serilog.Logfmt", + "OnDoubleQuotes": "Remove" + } + } + } + ] + } +} \ No newline at end of file diff --git a/ConfigurationTesting/resources/tmp/testlog.txt b/ConfigurationTesting/resources/tmp/testlog.txt deleted file mode 100644 index e69de29..0000000 diff --git a/ConfigurationTesting/resources/tmp/testlog_complex.txt b/ConfigurationTesting/resources/tmp/testlog_complex.txt deleted file mode 100644 index b56b33d..0000000 --- a/ConfigurationTesting/resources/tmp/testlog_complex.txt +++ /dev/null @@ -1,5 +0,0 @@ -ts=2024-09-22T22:46:31.8790575Z level=err msg="this is a test!" -ts=2024-09-22T22:47:22.1503711Z level=err complex="{ A = alpha, B = beta }" msg="this is a test!" -ts=2024-09-22T22:48:14.0440113Z level=err complex.name="Property DOUBLE QUOTES on it" complex.when="22/09/2024 22:48:14" complex.value=42 complex.sub.name=Test complex.sub.iv=32 msg="this is a test!" -ts=2024-09-22T22:48:46.1693296Z level=err complex.a=alpha complex.b=beta msg="this is a test!" -ts=2024-09-22T22:49:16.9228369Z level=err complex:a=alpha complex:b=beta msg="this is a test!" diff --git a/ConfigurationTesting/resources/tmp/testlog_preserved.txt b/ConfigurationTesting/resources/tmp/testlog_preserved.txt deleted file mode 100644 index 0ffe5f9..0000000 --- a/ConfigurationTesting/resources/tmp/testlog_preserved.txt +++ /dev/null @@ -1,5 +0,0 @@ -ts=2024-09-22T22:46:31.8648941Z level=Warning msg="this is a test!" -ts=2024-09-22T22:47:22.1338149Z level=Warning msg="this is a test!" -ts=2024-09-22T22:48:14.0272200Z level=Warning msg="this is a test!" -ts=2024-09-22T22:48:46.1494234Z level=Warning msg="this is a test!" -ts=2024-09-22T22:49:16.8977256Z level=Warning msg="this is a test!" From 1244713cd7fbf452e41db7bc8d26c4d0f1e3852b Mon Sep 17 00:00:00 2001 From: Sophie Rudd Date: Mon, 23 Sep 2024 00:34:44 +0100 Subject: [PATCH 3/3] Delete temporary folder after testing --- ConfigurationTesting/ConfigurationTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ConfigurationTesting/ConfigurationTests.cs b/ConfigurationTesting/ConfigurationTests.cs index 175b1a6..a5c7c79 100644 --- a/ConfigurationTesting/ConfigurationTests.cs +++ b/ConfigurationTesting/ConfigurationTests.cs @@ -164,6 +164,6 @@ public void EscapeDoubles() public void Dispose() { - Directory.Delete(basefolder + "\\resources\\tmp"); + Directory.Delete(basefolder + "\\resources\\tmp", true); } } \ No newline at end of file