Security 使用NLog时保护电子邮件密码

Security 使用NLog时保护电子邮件密码,security,email,logging,password-protection,nlog,Security,Email,Logging,Password Protection,Nlog,当使用NLog作为日志记录工具时,我们可以轻松地通过电子邮件发送消息,例如,这是使用Gmail作为smtp服务器的示例配置: <targets> <target name="gmail" type="Mail" smtpServer="smtp.gmail.com" smtpPort="587" smtpAuthentication="Basic" smtpUsername="user@gmail.com"

当使用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>

它就像一个符咒。 但在上面的示例中,密码以明文形式放在配置文件中。

是否有办法保护它?是的,您可以将NLog.config(如果此文件中有)移动到app.config,然后对app.config进行加密


您可以查看如何加密app.config。

您可以在代码中配置记录器。在那里,你甚至可以用十六进制编辑器对任何人隐藏你的密码!另外,没有人有机会弄乱你的配置文件

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")