I am trying to create a Log4Net wrapper interface. The code for the Interface:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PlayGround
{
public interface ILogger
{
void SetSource(string typeName);
/// <summary>
/// Provide Log "message" and/or "exception" data only
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
void Error(string message, Exception exception = null,
[System.Runtime.CompilerServices.CallerMemberName] string memberName = null,
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = null,
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0);
/// <summary>
/// Provide Log "message" and/or "exception" data only
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
void Warn(string message, Exception exception = null,
[System.Runtime.CompilerServices.CallerMemberName] string memberName = null,
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = null,
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0);
/// <summary>
/// Provide Log "message" and/or "exception" data only
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
void Debug(string message, Exception exception = null,
[System.Runtime.CompilerServices.CallerMemberName] string memberName = null,
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = null,
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0);
/// <summary>
/// Provide Log "message" and/or "exception" data only
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
void Info(string message, Exception exception = null,
[System.Runtime.CompilerServices.CallerMemberName] string memberName = null,
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = null,
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0);
/// <summary>
/// Provide Log "message" and/or "exception" data only
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
void Fatal(string message, Exception exception = null,
[System.Runtime.CompilerServices.CallerMemberName] string memberName = null,
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = null,
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0);
}
}
The implementation:
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PlayGround
{
class Log4NetLogger : ILogger
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Log4NetLogger));
private static readonly bool isErrorEnabled = log.IsErrorEnabled;
private static readonly bool isWarnEnabled = log.IsWarnEnabled;
private static readonly bool isDebugEnabled = log.IsDebugEnabled;
private static readonly bool isInfoEnabled = log.IsInfoEnabled;
private static readonly bool isFatalEnabled = log.IsFatalEnabled;
private string TypeName;
public void SetSource(string typeName)
{
TypeName = typeName;
}
public void Error(string message, Exception Exception = null, string memberName = null, string sourceFilePath = null, int sourceLineNumber = 0)
{
if (isErrorEnabled)
{
string Message = BuildSourceDetails(message, memberName, sourceFilePath, sourceLineNumber);
if (Exception != null)
{
Message += BuildExceptionMsg(Exception.Message);
}
log.Error(Message);
}
}
public void Warn(string message, Exception Exception = null, string memberName = null, string sourceFilePath = null, int sourceLineNumber = 0)
{
if (isWarnEnabled)
{
string Message = BuildSourceDetails(message, memberName, sourceFilePath, sourceLineNumber);
if (Exception != null)
{
Message += BuildExceptionMsg(Exception.Message);
}
log.Warn(Message);
}
}
public void Debug(string message, Exception Exception = null, string memberName = null, string sourceFilePath = null, int sourceLineNumber = 0)
{
if (isDebugEnabled)
{
string Message = BuildSourceDetails(message, memberName, sourceFilePath, sourceLineNumber);
if (Exception != null)
{
Message += BuildExceptionMsg(Exception.Message);
}
log.Debug(Message);
}
}
public void Info(string message, Exception Exception = null, string memberName = null, string sourceFilePath = null, int sourceLineNumber = 0)
{
if (isInfoEnabled)
{
string Message = BuildSourceDetails(message, memberName, sourceFilePath, sourceLineNumber);
if (Exception != null)
{
Message += BuildExceptionMsg(Exception.Message);
}
log.Info(Message);
}
}
public void Fatal(string message, Exception Exception = null, string memberName = null, string sourceFilePath = null, int sourceLineNumber = 0)
{
if (isFatalEnabled)
{
string Message = BuildSourceDetails(message, memberName, sourceFilePath, sourceLineNumber);
if (Exception != null)
{
Message += BuildExceptionMsg(Exception.Message);
}
log.Fatal(Message);
}
}
private string BuildSourceDetails(string message, string memberName, string sourceFilePath, int sourceLineNumber)
{
return "[Class: " + TypeName + " Member: " + memberName + " Source: " + sourceFilePath + " Line: " + sourceLineNumber + "] [" + message + "]";
}
private string BuildExceptionMsg(string message)
{
return " [System Exception: " + message + "] ";
}
}
}
I believe from a performance point of view the code works based on the online research I have carried out.
The question being; instead of using Attributes within the interface, is there an approach using C# Reflection so that I can move the code for Logging to the concrete implementation only? This way the interface is more generic?
Thank you kindly.
Aucun commentaire:
Enregistrer un commentaire