当使用NLog作为日志记录工具时,我们可以通过电子邮件轻松发送消息,例如,这是使用Gmail作为smtp服务器的示例配置:
<targets>
<target name="gmail" type="Mail"
smtpServer="smtp.gmail.com"
smtpPort="587"
smtpAuthentication="Basic"
smtpUsername="user@gmail.com"
smtpPassword="password"
enableSsl="true"
from="emailaddress@gmail.com"
to="recipient@example.com"
cc="alice@example.com;bob@example.com;charlie@example.com"
/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="gmail" />
</rules>
它就像一个魅力。 但在上面的示例中,密码以纯文本形式放在配置文件中 有没有办法以某种方式保护它?
答案 0 :(得分:1)
是的,你可以将NLog.config(如果你在这个文件中有它)移动到app.config,然后加密app.config。
您可以看到如何加密app.config here。
答案 1 :(得分:0)
您可以在代码中配置记录器。在那里,您甚至可以使用十六进制编辑器隐藏任何人的密码!另外,没有人有机会搞乱你的配置文件。
Public Class MyEmailLoggerClass
Public Sub New()
SetNlogConfiguration()
End Sub
Private Sub SetNlogConfiguration()
Private MyNLogConfiguration As New LoggingConfiguration()
Dim myemailtarget As New MailTarget()
Dim MailRule As New LoggingRule("myemail", LogLevel.Info, myemailtarget)
With myemailtarget
.SmtpServer = "mail.724hosting.com"
.SmtpAuthentication = SmtpAuthenticationMode.Basic
.SmtpUserName = "myemail" & "@" & "somewhere.com"
.SmtpPassword = "my" & "pass" & "word"
.SmtpPort = 587
'.Encoding = System.Text.Encoding.UTF8
.EnableSsl = true
.AddNewLines = True
.Html = True
.Layout = "${message}"
'message options
.To = "sometech" & "@" & "somewhere.com"
.cc = "bob@somewhere.com,harry@somewhereelse.com"
.From = "myapplication@here.com"
.Header = "<h2 style='color:red'>Message from myapplication!</h2>"
.Subject = "Report from " & myapplicationname & " on someone's computer" & ${date:F}"
.Body = "${message}"
End With
LogManager.Configuration = myNlogConfig
End Sub
End Class
要使用它,请将其放在要发送电子邮件的子站点中:
Public MainLogger As New MyEmailLoggerClass
Dim Logger = LogManager.GetLogger("myemail")
Logger.Info("Hi, this is a message from my application")