9

below config was my nlog setting, this setting send mail is OK .

<target name="mail" xsi:type="Mail"
            smtpServer="SMTP SERVER"
            smtpPort="25"
            smtpAuthentication="None"   
            enableSsl="false"   
            from="email address"  
            to="email address"
            html="true"
            encoding="UTF-8"
            addNewLines="true"
            replaceNewlineWithBrTagInHtml ="true"
            subject="SYSTEM MESSAGE:${machinename} 於 ${shortdate} ${time} create ${level} message "
            header="========================================================================="
            body="${newline} 
            time:${longdate} ${newline}${newline}
            Log level:${level:uppercase=true} ${newline}${newline}
            Logger:${logger} ${newline}${newline}
            Source:${callsite:className=true} ${newline}${newline}
            Exception:${exception:format=type} ${newline}${newline}
            Error message:${message} ${newline}${newline}"     
            footer="========================================================================="
    />

</targets>

 <rules>
    <logger name="*" minlevel="Fatal" writeTo="mail" />
 </rules>

but I want to send colorful mail. How to setting configure ?

Raghbendra Nayak
  • 1,606
  • 3
  • 22
  • 47
square
  • 123
  • 1
  • 1
  • 11
  • 1
    I was looking for a way to HTMLise my body of NLog Mail Target. Then I came at this. Your formatting looks awesome to me for now so I am using this for a while. Thanks bruh. – taimur alam Jul 06 '18 at 06:11

2 Answers2

3

For markup like colors in your e-mail, you need a html mail and CSS styling.

e.g. this html:

<body>
    <b style="color:red">Bold and red text</b>
</body>

You need to set the html option on the mail target to true and in your nlog.config you need to XML encode the html, so in result:

<target name="mail" xsi:type="Mail"
            html="true"
            ...

            body="&lt;body&gt;
&lt;b style=&quot;color:red&quot;&gt;Bold and red text&lt;/b&gt;
&lt;/body&gt;"
    />

Please note that not all CSS is supported in all email clients. See CSS Support Guide for Email Clients

Julian
  • 33,915
  • 22
  • 119
  • 174
-1

An easy approach is to log messages with the HTML already in the logged string, for example:

In the config file:

<targets>
   <target 
      name="mail" 
      xsi:type="Mail" 
      replaceNewlineWithBrTagInHtml="true"
      html="true"
      body="${message}"             
   />
</targets>
<rules>
    <logger name="*" minlevel="Info" writeTo="mail"></logger>
</rules>

In the code:

log.Info("<strong>My Bolded Message</string>");

Also, instead of inlining the HTML tags, one can use an HTML library like htmltags or even System.Xml.Linq to make it much more robust.

OfirD
  • 9,442
  • 5
  • 47
  • 90
  • This approach is problematic if you have different log targets. You'll pollute all your other logs with HTML-Markup. – display-name Jul 11 '22 at 07:31