-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using new logging framework Serilog do trace errors more easily
- Loading branch information
1 parent
91f935e
commit 901673d
Showing
24 changed files
with
316 additions
and
377 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,20 @@ | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace BuildVision.Common | ||
{ | ||
public static class ApplicationInfo | ||
{ | ||
public const string ApplicationName = "BuildVision"; | ||
|
||
public static FileVersionInfo GetHostVersionInfo() | ||
{ | ||
return Process.GetCurrentProcess().MainModule.FileVersionInfo; | ||
} | ||
|
||
public static Version GetPackageVersion(object package) | ||
{ | ||
return package.GetType().Assembly.GetName().Version; | ||
} | ||
} | ||
} |
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,64 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Threading.Tasks; | ||
using Serilog; | ||
|
||
namespace BuildVision.Common.Logging | ||
{ | ||
public static class ILoggerExtensions | ||
{ | ||
public static void Assert(this ILogger logger, bool condition, string messageTemplate) | ||
{ | ||
if (!condition) | ||
{ | ||
messageTemplate = "Assertion Failed: " + messageTemplate; | ||
#pragma warning disable Serilog004 // propertyValues might not be strings | ||
logger.Warning(messageTemplate); | ||
#pragma warning restore Serilog004 | ||
} | ||
} | ||
|
||
public static void Assert(this ILogger logger, bool condition, string messageTemplate, params object[] propertyValues) | ||
{ | ||
if (!condition) | ||
{ | ||
messageTemplate = "Assertion Failed: " + messageTemplate; | ||
#pragma warning disable Serilog004 // propertyValues might not be strings | ||
logger.Warning(messageTemplate, propertyValues); | ||
#pragma warning restore Serilog004 | ||
} | ||
} | ||
|
||
public static void Time(this ILogger logger, string name, Action method) | ||
{ | ||
var startTime = DateTime.Now; | ||
method(); | ||
logger.Verbose("{Name} took {Seconds} seconds", name, FormatSeconds(DateTime.Now - startTime)); | ||
} | ||
|
||
public static T Time<T>(this ILogger logger, string name, Func<T> method) | ||
{ | ||
var startTime = DateTime.Now; | ||
var value = method(); | ||
logger.Verbose("{Name} took {Seconds} seconds", name, FormatSeconds(DateTime.Now - startTime)); | ||
return value; | ||
} | ||
|
||
public static async Task TimeAsync(this ILogger logger, string name, Func<Task> methodAsync) | ||
{ | ||
var startTime = DateTime.Now; | ||
await methodAsync().ConfigureAwait(false); | ||
logger.Verbose("{Name} took {Seconds} seconds", name, FormatSeconds(DateTime.Now - startTime)); | ||
} | ||
|
||
public static async Task<T> TimeAsync<T>(this ILogger logger, string name, Func<Task<T>> methodAsync) | ||
{ | ||
var startTime = DateTime.Now; | ||
var value = await methodAsync().ConfigureAwait(false); | ||
logger.Verbose("{Name} took {Seconds} seconds", name, FormatSeconds(DateTime.Now - startTime)); | ||
return value; | ||
} | ||
|
||
static string FormatSeconds(TimeSpan timeSpan) => timeSpan.TotalSeconds.ToString("0.##", CultureInfo.InvariantCulture); | ||
} | ||
} |
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,13 @@ | ||
using System; | ||
using Serilog; | ||
|
||
namespace BuildVision.Common.Logging | ||
{ | ||
public static class Log | ||
{ | ||
private static Lazy<ILogger> Logger { get; } = new Lazy<ILogger>(() => LogManager.ForContext(typeof(Log))); | ||
|
||
public static void Assert(bool condition, string messageTemplate) | ||
=> Logger.Value.Assert(condition, messageTemplate); | ||
} | ||
} |
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,56 @@ | ||
using System; | ||
using System.IO; | ||
using Serilog; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace BuildVision.Common.Logging | ||
{ | ||
public static class LogManager | ||
{ | ||
#if DEBUG | ||
private static LogEventLevel DefaultLoggingLevel = LogEventLevel.Debug; | ||
#else | ||
private static LogEventLevel DefaultLoggingLevel = LogEventLevel.Information; | ||
#endif | ||
|
||
private static LoggingLevelSwitch LoggingLevelSwitch = new LoggingLevelSwitch(DefaultLoggingLevel); | ||
|
||
static Logger CreateLogger() | ||
{ | ||
var logPath = Path.Combine( | ||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), | ||
ApplicationInfo.ApplicationName, | ||
"extension.log"); | ||
|
||
const string outputTemplate = | ||
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{ProcessId:00000}] {Level:u4} [{ThreadId:00}] {ShortSourceContext,-25} {Message:lj}{NewLine}{Exception}"; | ||
|
||
return new LoggerConfiguration() | ||
.Enrich.WithProcessId() | ||
.Enrich.WithThreadId() | ||
.MinimumLevel.ControlledBy(LoggingLevelSwitch) | ||
.WriteTo.File(logPath, | ||
fileSizeLimitBytes: null, | ||
outputTemplate: outputTemplate, | ||
shared: true) | ||
.CreateLogger(); | ||
} | ||
|
||
public static void EnableTraceLogging(bool enable) | ||
{ | ||
var logEventLevel = enable ? LogEventLevel.Verbose : DefaultLoggingLevel; | ||
if (LoggingLevelSwitch.MinimumLevel != logEventLevel) | ||
{ | ||
ForContext(typeof(LogManager)).Information("Set Logging Level: {LogEventLevel}", logEventLevel); | ||
LoggingLevelSwitch.MinimumLevel = logEventLevel; | ||
} | ||
} | ||
|
||
static Lazy<Logger> Logger { get; } = new Lazy<Logger>(CreateLogger); | ||
|
||
public static ILogger ForContext<T>() => ForContext(typeof(T)); | ||
|
||
public static ILogger ForContext(Type type) => Logger.Value.ForContext(type).ForContext("ShortSourceContext", type.Name); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.