-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Add NLog logging adapter (#236)
- Loading branch information
1 parent
941e403
commit d27b0b4
Showing
6 changed files
with
406 additions
and
0 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,39 @@ | ||
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
namespace Splat.NLog | ||
{ | ||
/// <summary> | ||
/// NLog specific extensions for the Mutable Dependency Resolver. | ||
/// </summary> | ||
public static class MutableDependencyResolverExtensions | ||
{ | ||
/// <summary> | ||
/// Simple helper to initialize NLog within Splat with the Wrapping Full Logger. | ||
/// </summary> | ||
/// <remarks> | ||
/// You should configure NLog prior to calling this method. | ||
/// </remarks> | ||
/// <param name="instance"> | ||
/// An instance of Mutable Dependency Resolver. | ||
/// </param> | ||
/// <example> | ||
/// <code> | ||
/// Locator.CurrentMutable.UseNLogWithWrappingFullLogger(); | ||
/// </code> | ||
/// </example> | ||
public static void UseNLogWithWrappingFullLogger(this IMutableDependencyResolver instance) | ||
{ | ||
var funcLogManager = new FuncLogManager(type => | ||
{ | ||
var actualLogger = global::NLog.LogManager.GetLogger(type.ToString()); | ||
var miniLoggingWrapper = new NLogLogger(actualLogger); | ||
return new WrappingFullLogger(miniLoggingWrapper); | ||
}); | ||
|
||
instance.RegisterConstant(funcLogManager, typeof(ILogManager)); | ||
} | ||
} | ||
} |
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,78 @@ | ||
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Reflection; | ||
using NLog; | ||
|
||
namespace Splat.NLog | ||
{ | ||
/// <summary> | ||
/// NLog Logger taken from ReactiveUI 5. | ||
/// </summary> | ||
[DebuggerDisplay("Name={_inner.Name} Level={Level}")] | ||
public sealed class NLogLogger : ILogger | ||
{ | ||
private readonly global::NLog.ILogger _inner; | ||
private LogLevel _level; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NLogLogger"/> class. | ||
/// </summary> | ||
/// <param name="inner">The actual nlog logger.</param> | ||
/// <exception cref="ArgumentNullException">NLog logger not passed.</exception> | ||
public NLogLogger(global::NLog.ILogger inner) | ||
{ | ||
_inner = inner ?? throw new ArgumentNullException(nameof(inner)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public LogLevel Level | ||
{ | ||
get => _level; | ||
|
||
set | ||
{ | ||
// would it be better for ILogger to have a readonly property? | ||
// rather than a rather blunt way to adjust the level? | ||
// another considerationwas i didn't want to add a dependency on System.Reactive | ||
// up to you | ||
_level = value; | ||
foreach (var configurationLoggingRule in _inner.Factory.Configuration.LoggingRules) | ||
{ | ||
configurationLoggingRule.SetLoggingLevels(SplatLogLevelToNLogLevel(value), global::NLog.LogLevel.Fatal); | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Write(string message, LogLevel logLevel) | ||
{ | ||
_inner.Log(SplatLogLevelToNLogLevel(logLevel), message); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Write(string message, Type type, LogLevel logLevel) | ||
{ | ||
_inner.Log(SplatLogLevelToNLogLevel(logLevel), $"{type.Name}: {message}"); | ||
} | ||
|
||
private static global::NLog.LogLevel SplatLogLevelToNLogLevel(LogLevel logLevel) | ||
{ | ||
var mappings = new[] | ||
{ | ||
new Tuple<LogLevel, global::NLog.LogLevel>(LogLevel.Debug, global::NLog.LogLevel.Debug), | ||
new Tuple<LogLevel, global::NLog.LogLevel>(LogLevel.Info, global::NLog.LogLevel.Info), | ||
new Tuple<LogLevel, global::NLog.LogLevel>(LogLevel.Warn, global::NLog.LogLevel.Warn), | ||
new Tuple<LogLevel, global::NLog.LogLevel>(LogLevel.Error, global::NLog.LogLevel.Error), | ||
new Tuple<LogLevel, global::NLog.LogLevel>(LogLevel.Fatal, global::NLog.LogLevel.Fatal) | ||
}; | ||
|
||
return mappings.First(x => x.Item1 == logLevel).Item2; | ||
} | ||
} | ||
} |
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,17 @@ | ||
<Project Sdk="MSBuild.Sdk.Extras"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net461;uap10.0.16299;MonoAndroid80;Xamarin.iOS10;netstandard2.0</TargetFrameworks> | ||
<!--Xamarin.Mac20; --> | ||
<AssemblyName>Splat.NLog</AssemblyName> | ||
<RootNamespace>Splat</RootNamespace> | ||
<Authors>Paul Betts</Authors> | ||
<Description>NLog integrations for Splat</Description> | ||
<PackageId>Splat.NLog</PackageId> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="NLog" Version="4.5.0" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Splat\Splat.csproj" /> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.