Created
September 21, 2015 16:45
-
-
Save xinmyname/f3c77a4c45ee56035940 to your computer and use it in GitHub Desktop.
log4net logging interceptor
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 log4net; | |
| namespace Infrastructure | |
| { | |
| public interface IInterceptLogs | |
| { | |
| IList<string> Lines { get; } | |
| IList<Exception> Exceptions { get; } | |
| int DebugCount { get; } | |
| int InfoCount { get; } | |
| int WarnCount { get; } | |
| int ErrorCount { get; } | |
| int FatalCount { get; } | |
| void Reset(); | |
| void Debug(object message, Exception exception = null); | |
| void Info(object message, Exception exception = null); | |
| void Warn(object message, Exception exception = null); | |
| void Error(object message, Exception exception = null); | |
| void Fatal(object message, Exception exception = null); | |
| } | |
| public class LogInterceptor : IInterceptLogs | |
| { | |
| private readonly ILog _log; | |
| public IList<string> Lines { get; private set; } | |
| public IList<Exception> Exceptions { get; private set; } | |
| public int DebugCount { get; private set; } | |
| public int InfoCount { get; private set; } | |
| public int WarnCount { get; private set; } | |
| public int ErrorCount { get; private set; } | |
| public int FatalCount { get; private set; } | |
| public LogInterceptor(ILog log) | |
| { | |
| _log = log; | |
| Reset(); | |
| } | |
| public void Reset() | |
| { | |
| Lines = new List<string>(); | |
| Exceptions = new List<Exception>(); | |
| DebugCount = 0; | |
| InfoCount = 0; | |
| WarnCount = 0; | |
| InfoCount = 0; | |
| ErrorCount = 0; | |
| FatalCount = 0; | |
| } | |
| public void Debug(object message, Exception exception = null) | |
| { | |
| if (exception != null) | |
| _log.Debug(message, exception); | |
| else | |
| _log.Debug(message); | |
| if (_log.IsDebugEnabled) | |
| { | |
| AddLine(message, exception); | |
| DebugCount++; | |
| } | |
| } | |
| public void Info(object message, Exception exception = null) | |
| { | |
| if (exception != null) | |
| _log.Info(message, exception); | |
| else | |
| _log.Info(message); | |
| if (_log.IsInfoEnabled) | |
| { | |
| AddLine(message, exception); | |
| InfoCount++; | |
| } | |
| } | |
| public void Warn(object message, Exception exception = null) | |
| { | |
| if (exception != null) | |
| _log.Warn(message, exception); | |
| else | |
| _log.Warn(message); | |
| if (_log.IsWarnEnabled) | |
| { | |
| AddLine(message, exception); | |
| WarnCount++; | |
| } | |
| } | |
| public void Error(object message, Exception exception = null) | |
| { | |
| if (exception != null) | |
| _log.Error(message, exception); | |
| else | |
| _log.Error(message); | |
| if (_log.IsErrorEnabled) | |
| { | |
| AddLine(message, exception); | |
| ErrorCount++; | |
| } | |
| } | |
| public void Fatal(object message, Exception exception = null) | |
| { | |
| if (exception != null) | |
| _log.Fatal(message, exception); | |
| else | |
| _log.Fatal(message); | |
| if (_log.IsFatalEnabled) | |
| { | |
| AddLine(message, exception); | |
| FatalCount++; | |
| } | |
| } | |
| private void AddLine(object message, Exception exception = null) | |
| { | |
| if (exception != null) | |
| Exceptions.Add(exception); | |
| Lines.Add(message.ToString()); | |
| if (exception != null) | |
| { | |
| foreach (string line in exception.ToString().Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)) | |
| Lines.Add(line); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment