【问题标题】:Protecting an email password when using NLog使用 NLog 时保护电子邮件密码
【发布时间】:2011-07-08 07:33:47
【问题描述】:
当使用 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>
它就像一个魅力。
但在上面的示例中,密码以纯文本形式放在配置文件中。
有什么方法可以保护它吗?
【问题讨论】:
标签:
security
email
logging
password-protection
nlog
【解决方案1】:
是的,您可以将 NLog.config(如果您在此文件中有)移动到您的 app.config,然后加密您的 app.config。
您可以看到如何加密 app.config here。
【解决方案2】:
您可以在代码中配置记录器。在那里,您甚至可以使用十六进制编辑器向任何人隐藏您的密码!另外,没有人有机会弄乱您的配置文件。
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")