0
我想創建一個Log4Net包裝界面。該接口的代碼:C#反射比。方法屬性
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#反射的方法,以便我可以將用於記錄的代碼移到具體的實現中?這樣的界面更通用?
謝謝。
不知道真正想要什麼。但是如果你迫切需要使用反射,stacktrace(非常慢)總是可以提供相同的信息。 – leppie
@leppie:是的,由於性能考慮,我避免使用堆棧跟蹤。 – Guygar
猜你不希望在調用代碼中直接依賴NLog。你爲什麼不簡單地使用Facade或Adapter設計模式? –