我有一个像这样的Logger类:
public class Logger : ILogging
{
private ILogger _logger;
public Logger()
{
_logger = LoggerFactory<ILogging>.Resolve();
}
public void log(string priority, string message)
{
//to do the code here
}
}
这是我的配置文件:
<rules >
<logger name="Priority1" minlevel="Error" writeTo="logfile" />
<logger name="Priority2" minlevel="Warn" writeTo="logfile" />
<logger name="Priority3" minlevel="Debug" writeTo="logfile" />
<logger name="Priority4" minlevel="Trace" writeTo="logfile" />
我希望通过“Name”规则写入nlog中的特定函数 例如:如果用户呼叫
logger.log("Priority1","errorMessage");
该函数知道去logger.error(“error”)
我试图在谷歌中看到,但我没有看到任何好的解决方案
更新
public void log( Priority priority, string message)
{
string currentLogLevel;
_logger = NLog.LogManager.GetLogger(priority.ToString());
LogEventInfo logEvent = new LogEventInfo(currentLogLevel , _logger.Name, message);
_logger.Log(logEvent);
}
答案 0 :(得分:0)
答案 1 :(得分:0)
对OP的评论:
而不是
public void log( Priority priority, string message)
{
string currentLogLevel; // <= This can't work
_logger = NLog.LogManager.GetLogger(priority.ToString());
LogEventInfo logEvent = new LogEventInfo(currentLogLevel , _logger.Name, message);
_logger.Log(logEvent);
}
DO
public void log( Priority priority, string message)
{
LogLevel logLevel;
switch(priority)
{
case Priority.Priority1 : loglevel = LogLevel.Error; break;
case Priority.Priority2 : loglevel = LogLevel.Warn; break;
case Priority.Priority3 : loglevel = LogLevel.Debug; break;
case Priority.Priority4 : loglevel = LogLevel.Trace; break;
default: loglevel = LogLevel.None; break;
}
_logger = NLog.LogManager.GetLogger(priority.ToString());
LogEventInfo logEvent = new LogEventInfo(logLevel , _logger.Name, message);
_logger.Log(logEvent);
}
开始。
但我还建议那时只使用一个记录器并将其作为包装器的静态字段。
我个人通常使用NLog而不使用包装器。这让我可以自由地获得基于类的记录器和专业记录器,我可以在配置文件中进行路由和过滤。但是如果你需要或者想要使用包装器,我会考虑在消息本身没有这样做的情况下传递一些上下文和消息。