This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Log to file * Initialise logging * log file from commandline * log file name from settings file * fix * Stream logger * Write the message! * nulls in tests * static * Refactor for review
- Loading branch information
1 parent
c02b14d
commit 71cc2a6
Showing
23 changed files
with
197 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
|
||
namespace NuKeeper.Inspection.Logging | ||
{ | ||
public class ConfigurableLogger : INuKeeperLogger, IConfigureLogger | ||
{ | ||
private IInternalLogger _inner; | ||
|
||
public void Initialise(LogLevel logLevel, string filePath) | ||
{ | ||
var destination = string.IsNullOrWhiteSpace(filePath) ? | ||
LogDestination.Console : | ||
LogDestination.File; | ||
|
||
_inner = CreateLogger(logLevel, destination, filePath); | ||
} | ||
|
||
public void Error(string message, Exception ex) | ||
{ | ||
CheckLoggerCreated(); | ||
_inner.Error(message, ex); | ||
} | ||
|
||
public void Minimal(string message) | ||
{ | ||
CheckLoggerCreated(); | ||
_inner.Log(LogLevel.Minimal, message); | ||
} | ||
|
||
public void Normal(string message) | ||
{ | ||
CheckLoggerCreated(); | ||
_inner.Log(LogLevel.Normal, message); | ||
} | ||
|
||
public void Detailed(string message) | ||
{ | ||
CheckLoggerCreated(); | ||
_inner.Log(LogLevel.Detailed, message); | ||
} | ||
|
||
private void CheckLoggerCreated() | ||
{ | ||
if (_inner == null) | ||
{ | ||
_inner = CreateLogger(LogLevel.Detailed, LogDestination.Console, string.Empty); | ||
} | ||
} | ||
|
||
private static IInternalLogger CreateLogger( | ||
LogLevel logLevel, LogDestination destination, | ||
string filePath) | ||
{ | ||
switch (destination) | ||
{ | ||
case LogDestination.Console: | ||
return new ConsoleLogger(logLevel); | ||
|
||
case LogDestination.File: | ||
return new FileLogger(filePath, logLevel); | ||
|
||
default: | ||
throw new Exception($"Unknown log destination {destination}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace NuKeeper.Inspection.Logging | ||
{ | ||
public class FileLogger : IInternalLogger | ||
{ | ||
private readonly string _filePath; | ||
private readonly LogLevel _logLevel; | ||
|
||
public FileLogger(string filePath, LogLevel logLevel) | ||
{ | ||
_filePath = filePath; | ||
_logLevel = logLevel; | ||
} | ||
|
||
public void Error(string message, Exception ex) | ||
{ | ||
if (ex == null) | ||
{ | ||
WriteMessageToFile(message); | ||
} | ||
else | ||
{ | ||
Log(LogLevel.Quiet, $"{message} {ex.GetType().Name} : {ex.Message}"); | ||
if (_logLevel == LogLevel.Detailed) | ||
{ | ||
WriteMessageToFile(ex.StackTrace); | ||
} | ||
} | ||
} | ||
|
||
public void Log(LogLevel level, string message) | ||
{ | ||
if (_logLevel >= level) | ||
{ | ||
WriteMessageToFile(message); | ||
} | ||
} | ||
|
||
private void WriteMessageToFile(string message) | ||
{ | ||
using (var w = File.AppendText(_filePath)) | ||
{ | ||
w.WriteLine(message); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace NuKeeper.Inspection.Logging | ||
{ | ||
|
||
public interface IConfigureLogger | ||
{ | ||
void Initialise(LogLevel logLevel, string filePath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
|
||
namespace NuKeeper.Inspection.Logging | ||
{ | ||
public interface IInternalLogger | ||
{ | ||
void Error(string message, Exception ex); | ||
void Log(LogLevel level, string message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace NuKeeper.Inspection.Logging | ||
{ | ||
public enum LogDestination | ||
{ | ||
Console, | ||
File | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.