Created
June 21, 2022 20:56
-
-
Save brantburnett/dbcef2fcd69be9ece90fc140eb90e777 to your computer and use it in GitHub Desktop.
Log4Net to Microsoft Logging Adapter
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using log4net; | |
using log4net.Core; | |
using log4net.Repository; | |
using Microsoft.Extensions.Logging; | |
using ILogger = log4net.Core.ILogger; | |
namespace Logging | |
{ | |
internal class Log4NetLogger : Microsoft.Extensions.Logging.ILogger | |
{ | |
private readonly ILog _log; | |
public Log4NetLogger(ILog log) | |
{ | |
_log = log; | |
} | |
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) | |
{ | |
var logEvent = new LoggingEvent(new LoggingEventData | |
{ | |
Level = MapLevel(logLevel), | |
Message = formatter(state, exception), | |
ExceptionString = exception?.ToString(), | |
TimeStamp = DateTime.Now, | |
LoggerName = _log.Logger.Name, | |
LocationInfo = new LocationInfo(typeof(Log4NetLogger)) | |
}); | |
_log.Logger.Log(logEvent); | |
} | |
public bool IsEnabled(LogLevel logLevel) | |
{ | |
return _log.Logger.IsEnabledFor(MapLevel(logLevel)); | |
} | |
public IDisposable BeginScope<TState>(TState state) | |
{ | |
return new DisposableScope(); | |
} | |
private Level MapLevel(LogLevel level) | |
{ | |
switch (level) | |
{ | |
case LogLevel.Critical: | |
return Level.Critical; | |
case LogLevel.Error: | |
return Level.Error; | |
case LogLevel.Warning: | |
return Level.Warn; | |
case LogLevel.Information: | |
return Level.Info; | |
case LogLevel.Debug: | |
return Level.Debug; | |
case LogLevel.Trace: | |
return Level.Trace; | |
case LogLevel.None: | |
return Level.Debug; | |
} | |
return null; | |
} | |
private class DisposableScope : IDisposable | |
{ | |
public void Dispose() | |
{ | |
} | |
} | |
} | |
} |
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.Extensions.Logging; | |
namespace Logging | |
{ | |
public class Log4NetLoggerFactory : ILoggerFactory | |
{ | |
private ILoggerProvider _logProvider; | |
public void AddProvider(ILoggerProvider provider) | |
{ | |
_logProvider = provider; | |
} | |
public ILogger CreateLogger(string categoryName) | |
{ | |
if (_logProvider == null) | |
_logProvider = new Log4NetLoggerProvider(); | |
return _logProvider.CreateLogger(categoryName); | |
} | |
public void Dispose() | |
{ | |
_logProvider.Dispose(); | |
} | |
} | |
} |
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.Extensions.Logging; | |
namespace Logging | |
{ | |
internal class Log4NetLoggerProvider : ILoggerProvider | |
{ | |
public void Dispose() | |
{ | |
} | |
public ILogger CreateLogger(string categoryName) | |
{ | |
return new Log4NetLogger(log4net.LogManager.GetLogger(categoryName)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment