C# NLog集成:与方法或枚举使用接口?
要集成NLog,我们需要定义一个接口,并考虑两种方法。 我们正在使用C 方法1:C# NLog集成:与方法或枚举使用接口?,c#,oop,nlog,C#,Oop,Nlog,要集成NLog,我们需要定义一个接口,并考虑两种方法。 我们正在使用C 方法1: public interface ILoggingManager { void DoErrorLogging(type , string ) void DoErrorLogging(type , string , exception ) void DoTraceLogging(type , string ) void DoTrace Logging(type , strin
public interface ILoggingManager
{
void DoErrorLogging(type , string )
void DoErrorLogging(type , string , exception )
void DoTraceLogging(type , string )
void DoTrace Logging(type , string , exception )
// And so on for all the types that Nlog supports.
....
// Finally would have 10 methods defined in this interface.
}
方法2:
//Have an Enum defined for the logging levels
public enum LoggingLevel
{
Error,
Warn,
Info,
Debug,
Trace
}
public interface ILoggingManager
{
void DoLogging(type , LoggingLevel , string )
void DoLogging(type , LoggingLevel , exception )
}
问题:
我的回答并没有提供一个直接的解决方案,但仍然 我将使用一个现有的日志框架,其中包括所有接口。其余的是(或应该是)配置 我建议你要么用,要么用 如果您坚持使用自己的接口,那么我认为性能不是问题。要么将枚举值映射到NLog的适当方法(方法2),要么直接使用它(方法1) 方法2无疑要干净得多。我想把它改成:
public interface ILoggingManager
{
void DoLogging(type , LoggingLevel , string, /* optional */ exception )
}
我将描述我在方法2中看到的好处。有时,您事先不知道要使用的日志记录级别。假设您正在检查一个异常。有时会出现警告
,有时会出现错误
。如果使用方法2,代码重用会更简单:只需选择相关的枚举值,就可以进行设置。如果使用方法1,代码重用可能包括将映射到相关方法并调用它,这使得整个过程对于日志记录来说有点过于复杂
顺便问一下,这里的
类型是什么?消费类的类型?Ron,我们将使用Nlog框架,日志级别映射到Nlog的日志级别,只是我们正在编写一个接口,我们想知道哪种方法更好。为什么第二种方法更干净?@siva,你可以通过反射来检查消费类,但那是另一个故事。。。