我有一个使用NLog日志记录库的程序。我有一个包含目标和规则的配置文件,我以编程方式添加一些规则来写入数据库。到现在为止一切正常。
现在我想以编程方式更改配置。我使用SimpleConfigurator
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Fatal)
我的VB代码看起来像这样:
Dim target As MailTarget = New MailTarget()
target.Name = "Mailing"
target.Html = True
target.Body = "${message}"
target.SmtpServer = EmailSending.SendingServer
target.From = EmailSending.EmailSender
target.Encoding = System.Text.Encoding.UTF8
target.[To] = EmailSending.EmailReceiver
target.EnableSsl = EmailSending.EnableSSl
target.SmtpPort = EmailSending.sendingPort
target.SmtpUserName = EmailSending.EmailSender
target.SmtpPassword = EmailSending.EmailsenderPw
现在将这些内容添加到配置中:
LogManager.Configuration.AddTarget("Mailing", target)
Dim MailTarget As NLog.Targets.Target = LogManager.Configuration.FindTargetByName("Mailing")
Dim loggerRule As NLog.Config.LoggingRule = New NLog.Config.LoggingRule("*", LogLevel.Fatal, MailTarget)
LogManager.Configuration.LoggingRules.Add(loggerRule)
新目标和规则有效,但“simpleconfigurator”清除了之前的配置。我该如何预防呢? 谢谢你的帮助!
答案 0 :(得分:0)
使用SimpleConfigurator
确实跳过,您可以使用LogManager.Configuration.LoggingRules.Add
(或更简单的LogManager.Configuration.AddRule(...)
)
如果启用了自动重新加载,则会以编程方式添加规则和目标。您需要使用事件ConfigurationChanged
事件
这样的事情:
VB.NET:
Private Sub Init()
UpdateNLogConfig(Nothing, Nothing)
AddHandler LogManager.ConfigurationReloaded, AddressOf UpdateNLogConfig
End Sub
Private Sub UpdateNLogConfig(sender As Object, e As LoggingConfigurationReloadedEventArgs)
Dim target = New MailTarget()
target.Name = "Mailing"
target.Html = True
target.Body = "${message}"
target.SmtpServer = EmailSending.SendingServer
target.From = EmailSending.EmailSender
target.Encoding = System.Text.Encoding.UTF8
target.[To] = EmailSending.EmailReceiver
target.EnableSsl = EmailSending.EnableSSl
target.SmtpPort = EmailSending.sendingPort
target.SmtpUserName = EmailSending.EmailSender
target.SmtpPassword = EmailSending.EmailsenderPw
LogManager.Configuration.AddTarget("Mailing", target)
LogManager.Configuration.AddRuleForOneLevel(LogLevel.Fatal, target)
'or LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Fatal,target));
End Sub
C#
private void Init()
{
UpdateNLogConfig(null, null);
LogManager.ConfigurationReloaded += UpdateNLogConfig;
}
private void UpdateNLogConfig(object sender, LoggingConfigurationReloadedEventArgs e)
{
var target = new MailTarget();
target.Name = "Mailing";
target.Html = true;
target.Body = "${message}";
target.SmtpServer = EmailSending.SendingServer;
target.From = EmailSending.EmailSender;
target.Encoding = System.Text.Encoding.UTF8;
target.To = EmailSending.EmailReceiver;
target.EnableSsl = EmailSending.EnableSSl;
target.SmtpPort = EmailSending.sendingPort;
target.SmtpUserName = EmailSending.EmailSender;
target.SmtpPassword = EmailSending.EmailsenderPw;
LogManager.Configuration.AddTarget("Mailing", target);
LogManager.Configuration.AddRuleForOneLevel(LogLevel.Fatal, target); //or LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Fatal,target));
}