开发者

How to add a listener in NLog using Code

开发者 https://www.devze.com 2022-12-31 14:42 出处:网络
Consider NLog is already configured and logging messages to a file, I want to add a listener that will be called each time a message is logged. I read the documentation on NLog but what it says in doc

Consider NLog is already configured and logging messages to a file, I want to add a listener that will be called each time a message is logged. I read the documentation on NLog but what it says in document doesn't work. Does anyone know how开发者_开发知识库 to add a listener using Code in NLog. Thanks


Maybe the answer to this question will help.

I will repeat the suggested code here:

LoggingConfiguration config = LogManager.Configuration; 

var logFile = new FileTarget(); 
config.AddTarget("file", logFile); 

logFile.FileName = fileName + ".log"; 
logFile.Layout = "${date} | ${message}"; 

var rule = new LoggingRule("*", LogLevel.Info, logFile); 
config.LoggingRules.Add(rule); 

LogManager.Configuration = config; 

logger.Info("File converted!"); 

I haven't tried it, but if it works for you, you should consider voting up the answer in the linked thread. Note that it is ok if you want to vote my answer up as well.


Have you tried to use MessageCall target ?

The documentation is here:

http://nlog-project.org/wiki/MethodCall_target#Logging_to_a_static_method

0

精彩评论

暂无评论...
验证码 换一张
取 消