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

发布于 2024-11-19 07:30:10 字数 1402 浏览 3 评论 0原文

当使用 NLog 作为日志工具时,我们可以轻松地通过电子邮件发送消息,例如,这是使用 Gmail 作为 smtp 服务器的示例配置:

<targets>
<target name="gmail" type="Mail"
        smtpServer="smtp.gmail.com"
        smtpPort="587"
        smtpAuthentication="Basic"
        smtpUsername="[email protected]"
        smtpPassword="password"
        enableSsl="true"
        from="[email protected]"
        to="[email protected]"
        cc="[email protected];[email protected];[email protected]"
      />
</targets>
<rules>
 <logger name="*" minlevel="Debug" writeTo="gmail" />
</rules>

它就像一个魅力。 但在上面的示例中,密码以纯文本形式放入配置文件中。
有办法以某种方式保护它吗?

When using NLog as a logging instrument we can easily send messages via email, for example this is the example configuration for using Gmail as a smtp server:

<targets>
<target name="gmail" type="Mail"
        smtpServer="smtp.gmail.com"
        smtpPort="587"
        smtpAuthentication="Basic"
        smtpUsername="[email protected]"
        smtpPassword="password"
        enableSsl="true"
        from="[email protected]"
        to="[email protected]"
        cc="[email protected];[email protected];[email protected]"
      />
</targets>
<rules>
 <logger name="*" minlevel="Debug" writeTo="gmail" />
</rules>

It works like a charm.
But in the above example the password is put in plain text in a configuration file.
Is there a way to protect it somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还不是爱你 2024-11-26 07:30:10

是的,您可以将 NLog.config(如果此文件中有)移至您的 app.config,然后加密您的 app.config。

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

Yes, you can move the NLog.config (If you have it in this file) to your app.config and then encrypt you app.config.

You can see how to encrypt the app.config here.

顾铮苏瑾 2024-11-26 07:30:10

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

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 = "[email protected],[email protected]"
            .From = "[email protected]"
            .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")

You could configure the logger inside code. There, you can even hide your password from anyone with a hex-editor! Plus, nobody gets any chance to mess with your config file.

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 = "[email protected],[email protected]"
            .From = "[email protected]"
            .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

To use it, put this in the sub where you want to send email:

    Public MainLogger As New MyEmailLoggerClass
    Dim Logger = LogManager.GetLogger("myemail")
    Logger.Info("Hi, this is a message from my application")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文