Nlog电子邮件目标程序化
本文关键字:程序化 目标程序 目标 电子邮件 Nlog | 更新日期: 2023-09-27 17:58:02
我有一个使用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)
新的目标和规则有效,但"simpleconfigurationr"清除了以前的配置。我该如何防止这种情况发生?谢谢你的帮助!
-
跳过使用
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));
}