简体   繁体   English

如何使用config c#中的规则在NLog中发送特定功能

[英]how to send for specific functions in NLog using rules in config c#

I have a Logger class like this: 我有一个像这样的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
    }

} }

and this is my config file: 这是我的配置文件:

 <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" />

I want by "Name" rules to write to a specific function in nlog for example: if user call 我想通过“名称”规则将其写入nlog中的特定函数,例如:如果用户调用

 logger.log("Priority1","errorMessage");

the function know to go for logger.error("error") 该函数知道要使用logger.error(“ error”)

I tried to see in google but I didnt see any nice solve 我尝试在Google中查看,但没有找到任何好的解决方法

UPDATE UPDATE

      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);
    }

You can manage a wrapper class (factory), 您可以管理包装器类(工厂),

See example in: 请参阅以下示例:

How to retain callsite information when wrapping NLog 包装NLog时如何保留呼叫站点信息

Following the comments to OP: 在对OP的评论之后:

Instead of 代替

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);
}

for a start. 作为一个开始。

But I also suggest to then use only one logger and have that as a static field of your wrapper. 但是我也建议仅使用一个记录器,并将其作为包装器的静态字段。

I personally use NLog without wrapper, usually. 通常,我个人使用不带包装的NLog。 That gives me the freedom to get class-based loggers and specialized loggers that I can route and filter in the config file. 这使我可以自由获取基于类的记录器和专用记录器,可以在配置文件中进行路由和过滤。 But if you need or want to use a wrapper, I'd think about passing some context along with the message if you are not doing that in the message itself. 但是,如果您需要或想要使用包装器,那么如果您不在消息本身中这样做,我会考虑将一些上下文与消息一起传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM