我正在尝试创建一个Log4Net包装器接口。接口的代码:
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);
}
}
实施:
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 + "] ";
}
}
}
从性能的角度来看,我相信代码的工作基于我所进行的在线研究。
问题是;而不是在界面中使用属性,是否有使用C#Reflection的方法,以便我可以将Logging的代码仅移动到具体实现?这种界面更通用吗?
谢天谢地。
答案 0 :(得分:0)
你可以使用两种设计模式中的任何一种,我更喜欢适配器。基本上,您正在创建一个界面,该界面通过底层日志记录提供程序Nlog或Serilog创建抽象。
http://www.dofactory.com/net/facade-design-pattern
http://www.dofactory.com/net/adapter-design-pattern
但我相信这个问题已经解决了一百万次。看看https://github.com/net-commons/common-logging。