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

问题描述:

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

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

它的作用就像一个魅力。
但是在上面的例子中,密码在配置文件中以纯文本形式显示。

有没有办法保护它?

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?

是的,您可以移动NLog.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.

您可以看到如何加密app.config 这里

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