Skip to content

Commit 6dd5e33

Browse files
committed
remove old Logger implementation, implement FileAppender
1 parent 11346f2 commit 6dd5e33

File tree

13 files changed

+68
-146
lines changed

13 files changed

+68
-146
lines changed

src/GitVersionCore.Tests/ModuleInitializer.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/GitVersionCore.Tests/TestBase.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,5 @@ namespace GitVersionCore.Tests
22
{
33
public class TestBase
44
{
5-
static TestBase()
6-
{
7-
ModuleInitializer.Initialize();
8-
}
9-
105
}
116
}

src/GitVersionCore/Configuration/ConfigFileLocator.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@
44

55
namespace GitVersion.Configuration
66
{
7-
public interface IConfigFileLocator
8-
{
9-
bool HasConfigFileAt(string workingDirectory);
10-
string GetConfigFilePath(string workingDirectory);
11-
void Verify(string workingDirectory, string projectRootDirectory);
12-
string SelectConfigFilePath(GitPreparer gitPreparer);
13-
Config ReadConfig(string workingDirectory);
14-
void Verify(GitPreparer gitPreparer);
15-
}
16-
177
public abstract class ConfigFileLocator : IConfigFileLocator
188
{
199
protected readonly IFileSystem FileSystem;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace GitVersion.Configuration
2+
{
3+
public interface IConfigFileLocator
4+
{
5+
bool HasConfigFileAt(string workingDirectory);
6+
string GetConfigFilePath(string workingDirectory);
7+
void Verify(string workingDirectory, string projectRootDirectory);
8+
string SelectConfigFilePath(GitPreparer gitPreparer);
9+
Config ReadConfig(string workingDirectory);
10+
void Verify(GitPreparer gitPreparer);
11+
}
12+
}

src/GitVersionCore/Helpers/Logger.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
11
using System;
2+
using System.IO;
3+
using System.Threading;
24

35
namespace GitVersion.Log
46
{
57
public class FileAppender : ILogAppender
68
{
9+
private static readonly ReaderWriterLock locker = new ReaderWriterLock();
10+
private readonly string filePath;
11+
12+
public FileAppender(string filePath)
13+
{
14+
this.filePath = filePath;
15+
16+
var logFile = new FileInfo(Path.GetFullPath(filePath));
17+
18+
// NOTE: logFile.Directory will be null if the path is i.e. C:\logfile.log. @asbjornu
19+
logFile.Directory?.Create();
20+
if (logFile.Exists) return;
21+
22+
using (logFile.CreateText()) { }
23+
}
24+
725
public void WriteTo(LogLevel level, string message)
826
{
9-
throw new NotImplementedException();
27+
try
28+
{
29+
if (level != LogLevel.None)
30+
{
31+
WriteLogEntry(filePath, message);
32+
}
33+
}
34+
catch (Exception ex)
35+
{
36+
//
37+
}
38+
}
39+
40+
private static void WriteLogEntry(string logFilePath, string str)
41+
{
42+
var contents = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}\t\t{str}\r\n";
43+
File.AppendAllText(logFilePath, contents);
1044
}
1145
}
1246
}

src/GitVersionCore/Log/ILog.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public interface ILog
77
Verbosity Verbosity { get; set; }
88
void Write(Verbosity verbosity, LogLevel level, string format, params object[] args);
99
IDisposable IndentLog(string operationDescription);
10+
void AddLogAppender(ILogAppender logAppender);
1011
}
1112
}

src/GitVersionCore/Log/Log.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Globalization;
4+
using System.Linq;
45
using System.Text;
56
using System.Text.RegularExpressions;
67

78
namespace GitVersion.Log
89
{
910
public sealed class Log : ILog
1011
{
11-
private readonly IEnumerable<ILogAppender> appenders;
12+
private IEnumerable<ILogAppender> appenders;
1213
private readonly Regex ObscurePasswordRegex = new Regex("(https?://)(.+)(:.+@)", RegexOptions.Compiled);
1314
private readonly StringBuilder sb;
1415
private string indent = string.Empty;
@@ -52,6 +53,11 @@ public IDisposable IndentLog(string operationDescription)
5253
});
5354
}
5455

56+
public void AddLogAppender(ILogAppender logAppender)
57+
{
58+
appenders = appenders.Concat(new[] { logAppender });
59+
}
60+
5561
public override string ToString()
5662
{
5763
return sb.ToString();

src/GitVersionCore/Log/NullLog.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public IDisposable IndentLog(string operationDescription)
1515
return Disposable.Empty;
1616
}
1717

18+
public void AddLogAppender(ILogAppender logAppender)
19+
{
20+
}
21+
1822
public string Indent { get; set; }
1923
}
2024
}

src/GitVersionCore/VersionCalculation/MetaDataCalculator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using LibGit2Sharp;
33
using GitVersion.Helpers;
4+
using GitVersion.Log;
45

56
namespace GitVersion.VersionCalculation
67
{
@@ -17,7 +18,7 @@ public SemanticVersionBuildMetaData Create(Commit baseVersionSource, GitVersionC
1718

1819
var commitLog = context.Repository.Commits.QueryBy(qf);
1920
var commitsSinceTag = commitLog.Count();
20-
Logger.Info($"{commitsSinceTag} commits found between {baseVersionSource.Sha} and {context.CurrentCommit.Sha}");
21+
context.Log.Info($"{commitsSinceTag} commits found between {baseVersionSource.Sha} and {context.CurrentCommit.Sha}");
2122

2223
var shortSha = context.Repository.ObjectDatabase.ShortenObjectId(context.CurrentCommit);
2324
return new SemanticVersionBuildMetaData(

src/GitVersionExe/ExecCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private static bool RunMsBuildIfNeeded(Arguments args, string workingDirectory,
125125

126126
log.Info($"Launching build tool {BuildTool} \"{args.Proj}\" {args.ProjArgs}");
127127
var results = ProcessHelper.Run(
128-
Logger.Info, Logger.Error,
128+
m => log.Info(m), m => log.Error(m),
129129
null, BuildTool, $"\"{args.Proj}\" {args.ProjArgs}", workingDirectory,
130130
GetEnvironmentalVariables(variables));
131131

@@ -141,7 +141,7 @@ private static bool RunExecCommandIfNeeded(Arguments args, string workingDirecto
141141

142142
log.Info($"Launching {args.Exec} {args.ExecArgs}");
143143
var results = ProcessHelper.Run(
144-
Logger.Info, Logger.Error,
144+
m => log.Info(m), m => log.Error(m),
145145
null, args.Exec, args.ExecArgs, workingDirectory,
146146
GetEnvironmentalVariables(variables));
147147

src/GitVersionExe/GitVersionApplication.cs

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
43
using System.Reflection;
54
using GitVersion.Common;
@@ -16,8 +15,8 @@ public class GitVersionApplication : IGitVersionApplication
1615
private readonly IFileSystem fileSystem;
1716
private readonly IEnvironment environment;
1817
private readonly ILog log;
19-
private IHelpWriter helpWriter;
20-
private IVersionWriter versionWriter;
18+
private readonly IHelpWriter helpWriter;
19+
private readonly IVersionWriter versionWriter;
2120

2221
public GitVersionApplication(IFileSystem fileSystem, IEnvironment environment, ILog log)
2322
{
@@ -150,53 +149,15 @@ private static void VerifyConfiguration(Arguments arguments, ILog log)
150149

151150
private static void ConfigureLogging(Arguments arguments, ILog log)
152151
{
153-
var writeActions = new List<Action<string>>
154-
{
155-
s => log.Info(s)
156-
};
157-
158152
if (arguments.Output == OutputType.BuildServer || arguments.LogFilePath == "console" || arguments.Init)
159153
{
160-
writeActions.Add(Console.WriteLine);
154+
log.AddLogAppender(new ConsoleAppender());
161155
}
162156

163-
Exception exception = null;
164157
if (arguments.LogFilePath != null && arguments.LogFilePath != "console")
165158
{
166-
try
167-
{
168-
var logFileFullPath = Path.GetFullPath(arguments.LogFilePath);
169-
var logFile = new FileInfo(logFileFullPath);
170-
171-
// NOTE: logFile.Directory will be null if the path is i.e. C:\logfile.log. @asbjornu
172-
logFile.Directory?.Create();
173-
174-
using (logFile.CreateText())
175-
{
176-
}
177-
178-
writeActions.Add(x => WriteLogEntry(arguments, x));
179-
}
180-
catch (Exception ex)
181-
{
182-
exception = ex;
183-
}
159+
log.AddLogAppender(new FileAppender(arguments.LogFilePath));
184160
}
185-
186-
Logger.SetLoggers(
187-
s => writeActions.ForEach(a => { if (arguments.Verbosity >= Verbosity.Diagnostic) a(s); }),
188-
s => writeActions.ForEach(a => { if (arguments.Verbosity >= Verbosity.Normal) a(s); }),
189-
s => writeActions.ForEach(a => { if (arguments.Verbosity >= Verbosity.Minimal) a(s); }),
190-
s => writeActions.ForEach(a => { if (arguments.Verbosity >= Verbosity.Quiet) a(s); }));
191-
192-
if (exception != null)
193-
log.Error($"Failed to configure logging for '{arguments.LogFilePath}': {exception.Message}");
194-
}
195-
196-
private static void WriteLogEntry(Arguments arguments, string s)
197-
{
198-
var contents = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}\t\t{s}\r\n";
199-
File.AppendAllText(arguments.LogFilePath, contents);
200161
}
201162
}
202163
}

src/GitVersionExe/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Program
1010
{
1111
static void Main()
1212
{
13-
var log = new Log.Log(/*new ConsoleAppender()*/);
13+
var log = new Log.Log();
1414
var fileSystem = new FileSystem();
1515
var environment = new Environment();
1616
var argumentParser = new ArgumentParser(fileSystem, log);

0 commit comments

Comments
 (0)