Skip to content

Commit 20ea687

Browse files
authored
(GH-179) Replace custom logging with serilog implementation (#180)
(GH-179) Replace custom logging with serilog implementation
2 parents 5ec2277 + fb3e8c6 commit 20ea687

26 files changed

+343
-314
lines changed

Source/.editorconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,7 @@ dotnet_naming_style.begins_with_i.capitalization = pascal_case
188188
dotnet_diagnostic.CA1305.severity = none
189189

190190
# CA1031: Do not catch general exception types
191-
dotnet_diagnostic.CA1031.severity = silent
191+
dotnet_diagnostic.CA1031.severity = silent
192+
193+
# CA1707: Identifiers should not contain underscores
194+
dotnet_diagnostic.CA1707.severity = silent

Source/Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
<PrivateAssets>all</PrivateAssets>
1818
</PackageReference>
19+
<PackageReference Include="Serilog" Version="2.9.0" />
1920
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
2021
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2122
<PrivateAssets>all</PrivateAssets>

Source/GitReleaseManager.Cli/Directory.Build.props

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@
22
<PropertyGroup>
33
<BaseIntermediateOutputPath>$(MSBuildThisFileDirectory)obj\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
44
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<PackageReference Include="Destructurama.Attributed" Version="2.0.0" />
8+
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
9+
<PackageReference Include="Serilog.Sinks.Debug" Version="1.0.1" Privateassets="All" />
10+
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
11+
</ItemGroup>
512
</Project>

Source/GitReleaseManager.Cli/GitReleaseManager.Cli.csproj

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,10 @@
44
<OutputType>Exe</OutputType>
55
<AssemblyName>GitReleaseManager</AssemblyName>
66
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
7-
<IsWebBootstrapper>false</IsWebBootstrapper>
8-
<PublishUrl>publish\</PublishUrl>
9-
<Install>true</Install>
10-
<InstallFrom>Disk</InstallFrom>
11-
<UpdateEnabled>false</UpdateEnabled>
12-
<UpdateMode>Foreground</UpdateMode>
13-
<UpdateInterval>7</UpdateInterval>
14-
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
15-
<UpdatePeriodically>false</UpdatePeriodically>
16-
<UpdateRequired>false</UpdateRequired>
17-
<MapFileExtensions>true</MapFileExtensions>
18-
<ApplicationRevision>0</ApplicationRevision>
19-
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
20-
<UseApplicationTrust>false</UseApplicationTrust>
21-
<BootstrapperEnabled>true</BootstrapperEnabled>
227
<Title>GitReleaseManager.Cli</Title>
238
<Description>Create release notes in markdown given a milestone</Description>
249
<IsPackable>false</IsPackable>
25-
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
10+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
2611
</PropertyGroup>
2712
<ItemGroup>
2813
<Compile Include="..\SolutionInfo.cs" Link="SolutionInfo.cs" />
@@ -33,5 +18,6 @@
3318
<ItemGroup>
3419
<PackageReference Include="CommandLineParser" Version="2.6.0" />
3520
<PackageReference Include="Octokit" Version="0.36.0" />
21+
<PackageReference Include="seriloganalyzer" Version="0.15.0" />
3622
</ItemGroup>
3723
</Project>

Source/GitReleaseManager.Cli/GlobalSuppressions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
// "In Suppression File".
1515
// You do not need to add suppressions to this file manually.
1616

17-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2243:AttributeStringLiteralsShouldParseCorrectly", Justification = "This was in relation to the Information Version Number generated by GitVersion, which we want to leave as is.")]
17+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Wrong Usage", "DF0037:Marks undisposed objects assinged to a property, originated from a method invocation.", Justification = "Cleanup happens at the end of the application.", Scope = "member", Target = "~M:GitReleaseManager.Cli.Program.ConfigureLogging(GitReleaseManager.Cli.Options.BaseSubOptions)")]
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="LogConfiguration.cs" company="GitTools Contributors">
3+
// Copyright (c) 2015 - Present - GitTools Contributors
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace GitReleaseManager.Cli.Logging
8+
{
9+
using System.Diagnostics;
10+
using System.Text;
11+
using Destructurama;
12+
using GitReleaseManager.Cli.Options;
13+
using Octokit;
14+
using Serilog;
15+
using Serilog.Events;
16+
using Serilog.Sinks.SystemConsole.Themes;
17+
18+
public static class LogConfiguration
19+
{
20+
private const string CONSOLE_FULL_TEMPLATE = "[{Level:u3}] " + CONSOLE_INFO_TEMPLATE;
21+
private const string CONSOLE_INFO_TEMPLATE = "{Message:l}{NewLine}{Exception}";
22+
private const string FILE_TEMPLATE = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}";
23+
private static readonly ConsoleTheme _consoleTheme = AnsiConsoleTheme.Code;
24+
25+
public static void ConfigureLogging(BaseSubOptions options)
26+
{
27+
var config = new LoggerConfiguration()
28+
.MinimumLevel.Verbose()
29+
.Destructure.UsingAttributes()
30+
.Destructure.ByTransforming<ReleaseAssetUpload>(asset => new { asset.FileName, asset.ContentType });
31+
32+
CreateDebugLogger(config);
33+
CreateConsoleInformationLogger(config, CONSOLE_INFO_TEMPLATE, _consoleTheme);
34+
CreateConsoleFullLogger(config, CONSOLE_FULL_TEMPLATE, _consoleTheme, options);
35+
36+
if (!string.IsNullOrEmpty(options.LogFilePath))
37+
{
38+
CreateFileLogger(config, options.LogFilePath, FILE_TEMPLATE);
39+
}
40+
41+
Log.Logger = config.CreateLogger();
42+
}
43+
44+
private static void CreateConsoleFullLogger(LoggerConfiguration config, string consoleTemplate, ConsoleTheme consoleTheme, BaseSubOptions options)
45+
{
46+
config.WriteTo.Logger((config) => config
47+
.Filter.ByExcluding((logEvent) => logEvent.Level == LogEventLevel.Information)
48+
.Filter.ByExcluding((logEvent) => !options.Debug && logEvent.Level == LogEventLevel.Debug)
49+
.Filter.ByExcluding((logEvent) => !options.Verbose && logEvent.Level == LogEventLevel.Verbose)
50+
.WriteTo.Console(
51+
outputTemplate: consoleTemplate,
52+
standardErrorFromLevel: LogEventLevel.Warning,
53+
theme: consoleTheme));
54+
}
55+
56+
private static void CreateConsoleInformationLogger(LoggerConfiguration config, string consoleTemplate, ConsoleTheme consoleTheme)
57+
{
58+
config.WriteTo.Logger((config) => config
59+
.Filter.ByIncludingOnly((logEvent) => logEvent.Level == LogEventLevel.Information)
60+
.WriteTo.Console(
61+
outputTemplate: consoleTemplate,
62+
theme: consoleTheme));
63+
}
64+
65+
private static void CreateFileLogger(LoggerConfiguration config, string logFilePath, string logTemplate)
66+
{
67+
config.WriteTo.File(logFilePath, outputTemplate: logTemplate, encoding: new UTF8Encoding(false));
68+
}
69+
70+
[Conditional("DEBUG")]
71+
private static void CreateDebugLogger(LoggerConfiguration config)
72+
{
73+
config.WriteTo.Debug();
74+
}
75+
}
76+
}

Source/GitReleaseManager.Cli/Options/BaseSubOptions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ namespace GitReleaseManager.Cli.Options
1010

1111
public abstract class BaseSubOptions
1212
{
13-
[Option('d', "targetDirectory", HelpText = "The directory on which GitReleaseManager should be executed. Defaults to current directory.", Required = false)]
14-
public string TargetDirectory { get; set; }
13+
[Option("debug", HelpText = "Enable debugging console output")]
14+
public bool Debug { get; set; }
1515

1616
[Option('l', "logFilePath", HelpText = "Path to where log file should be created. Defaults to logging to console.", Required = false)]
1717
public string LogFilePath { get; set; }
1818

1919
[Option("no-logo", HelpText = "Disables the generation of the GRM commandline logo.", Required = false)]
2020
public bool NoLogo { get; set; }
21+
22+
[Option('d', "targetDirectory", HelpText = "The directory on which GitReleaseManager should be executed. Defaults to current directory.", Required = false)]
23+
public string TargetDirectory { get; set; }
24+
25+
[Option("verbose", HelpText = "Enabled verbose console output")]
26+
public bool Verbose { get; set; }
2127
}
2228
}

Source/GitReleaseManager.Cli/Options/BaseVcsSubOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
namespace GitReleaseManager.Cli.Options
77
{
88
using CommandLine;
9+
using Destructurama.Attributed;
910

1011
public abstract class BaseVcsOptions : BaseSubOptions
1112
{
1213
[Option('u', "username", HelpText = "The username to access Version Control System with.", Required = true, SetName = "Basic Auth")]
1314
public string UserName { get; set; }
1415

16+
[LogMasked(Text = "[REDACTED]")]
1517
[Option('p', "password", HelpText = "The password to access Version Control System with.", Required = true, SetName = "Basic Auth")]
1618
public string Password { get; set; }
1719

20+
[LogMasked(Text = "[REDACTED]")]
1821
[Option("token", HelpText = "The Access Token to access Version Control System with.", Required = true, SetName = "OAuth flow")]
1922
public string Token { get; set; }
2023

0 commit comments

Comments
 (0)