![](/img/trans.png)
[英]Get a method with a single or no attributes with Reflection in C#
[英]C# Reflection Vs. method Attributes
我正在尝试创建一个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#反射的方法,而不是在接口内使用属性,因此我可以将用于日志记录的代码仅移至具体实现? 这样的接口更通用吗?
非常感谢你。
您可以使用两种设计模式中的任何一种,我更喜欢使用适配器。 本质上,您正在创建一个接口,该接口在基础日志记录提供程序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 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.