Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the ability to use NLog #19

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
image: gitpod/workspace-dotnet
1 change: 1 addition & 0 deletions Source/Dna.Framework/Dna.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
<PackageReference Include="NLog" Version="4.7.8" />
</ItemGroup>
</Project>
29 changes: 29 additions & 0 deletions Source/Dna.Framework/Logging/NLog/NLogConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.Extensions.Logging;

namespace Dna
{
/// <summary>
/// The configuration for the NLog logger
/// </summary>
public class NLogConfiguration
{
#region Public Properties

/// <summary>
/// The level of log that should be processed
/// </summary>
public LogLevel LogLevel { get; set; } = LogLevel.Trace;

/// <summary>
/// Whether to log the time as part of the message
/// </summary>
public bool LogTime { get; set; } = true;

/// <summary>
/// Indicates if the log level should be output as part of the log message
/// </summary>
public bool OutputLogLevel { get; set; } = true;

#endregion
}
}
19 changes: 19 additions & 0 deletions Source/Dna.Framework/Logging/NLog/NLogExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.Extensions.Logging;

namespace Dna
{
public static class NLogExtensions
{
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string path, NLogConfiguration configuration = null)
{
if (configuration == null)
{
configuration = new NLogConfiguration();
}

builder.AddProvider(new NLogProvider(path, configuration));

return builder;
}
}
}
95 changes: 95 additions & 0 deletions Source/Dna.Framework/Logging/NLog/NLogLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Microsoft.Extensions.Logging;
using System;
using System.IO;

namespace Dna
{
public class NLogLogger : ILogger
{
protected NLogConfiguration mConfiguration;

private NLog.Logger mLogger;

public NLogLogger(string categoryName, string filePath, NLogConfiguration configuration)
{
mConfiguration = configuration;

var config = new NLog.Config.LoggingConfiguration();
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = Path.GetFullPath(filePath) };
var logconsole = new NLog.Targets.ConsoleTarget("logconsole");

config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, logfile);
config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, logconsole);

// Apply config
NLog.LogManager.Configuration = config;

mLogger = NLog.LogManager.GetLogger(categoryName);
}
public IDisposable BeginScope<TState>(TState state)
{
return null;
}

public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel)
{
return logLevel >= mConfiguration.LogLevel;
}

public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
// if we should log
if (!IsEnabled(logLevel))
{
// Return
return;
}

// Get current time
var currentTime = DateTimeOffset.Now.ToString("yyyy-MM-dd hh:mm:ss");

// Prepend log level
var logLevelString = mConfiguration.OutputLogLevel ? $"[{logLevel.ToString().ToUpper()}] " : "";

// prepend log level
var timeLogString = mConfiguration.LogTime ? $"[{currentTime}] " : "";

// Get the formatted message string
var message = formatter(state, exception);

// Write the message
var output = $"{logLevelString}{timeLogString}{message}";

mLogger.Log(ConvertLogLevel(logLevel), output);
}


/// <summary>
/// Convert log level to NLog variant.
/// </summary>
/// <param name="logLevel">level to be converted.</param>
/// <returns></returns>
private static NLog.LogLevel ConvertLogLevel(Microsoft.Extensions.Logging.LogLevel logLevel)
{
switch (logLevel)
{
case Microsoft.Extensions.Logging.LogLevel.Trace:
return NLog.LogLevel.Trace;
case Microsoft.Extensions.Logging.LogLevel.Debug:
return NLog.LogLevel.Debug;
case Microsoft.Extensions.Logging.LogLevel.Information:
return NLog.LogLevel.Info;
case Microsoft.Extensions.Logging.LogLevel.Warning:
return NLog.LogLevel.Warn;
case Microsoft.Extensions.Logging.LogLevel.Error:
return NLog.LogLevel.Error;
case Microsoft.Extensions.Logging.LogLevel.Critical:
return NLog.LogLevel.Fatal;
case Microsoft.Extensions.Logging.LogLevel.None:
return NLog.LogLevel.Off;
default:
return NLog.LogLevel.Debug;
}
}
}
}
33 changes: 33 additions & 0 deletions Source/Dna.Framework/Logging/NLog/NLogProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;

namespace Dna
{
public class NLogProvider : ILoggerProvider
{
protected string mFilePath;

protected readonly NLogConfiguration mConfiguration;

protected readonly ConcurrentDictionary<string, NLogLogger> mLoggers = new ConcurrentDictionary<string, NLogLogger>();

public NLogProvider(string path, NLogConfiguration configuration)
{
// Set the configuration
mConfiguration = configuration;

// Set the path
mFilePath = path;
}

public ILogger CreateLogger(string categoryName)
{
return mLoggers.GetOrAdd(categoryName, name => new NLogLogger(name, mFilePath, mConfiguration));
}

public void Dispose()
{
mLoggers.Clear();
}
}
}