Skip to content

Commit 8dbb4f8

Browse files
committed
Updated Blog Post 4.1 - Added section on Email Notifications
1 parent c7f41bd commit 8dbb4f8

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

Blog 4.1 - Updating Logging.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ In Global.asax.cs, call the logging configuration method in the Application_OnSt
6060

6161
Now that NLog is configured, we can start replacing the old custom logging code.
6262

63-
Starting with the code in Globa.asax.cs, replace the logging code with the new NLog logging code:
63+
Starting with the code in Global.asax.cs, replace the logging code with the new NLog logging code:
6464

6565
Logger logger = LogManager.GetCurrentClassLogger();
6666
logger.Fatal(exc);
@@ -77,6 +77,29 @@ Now that the error logging is working as expected, let's replace the code in Uti
7777
Logger log = LogManager.GetCurrentClassLogger();
7878
log.Debug(s);
7979

80-
Finally, we can delete the method Util.get_log_file_path since it is no longer used.
80+
We can delete the method Util.get_log_file_path since it is no longer used.
8181

8282
[View the commit](https://github.com/dpaquette/BugTracker.NET/commit/dd2ea87538c3c48f6a3a44220a14d51e38124fe6)
83+
84+
###Email Notifications
85+
There is a large block of code in Global.asax.cs that deals with sending out email notifications where unhandled exceptions occur. This logic is easily replaced using the Mail Target in NLog. By adding the following code to the LoggingConfig.Configure() method, we can delete all the custom email logic.
86+
87+
var mailTarget = new MailTarget
88+
{
89+
UseSystemNetMailSettings = true,
90+
To = Util.get_setting("ErrorEmailTo", ""),
91+
From = Util.get_setting("ErrorEmailFrom", ""),
92+
Subject = "BTNET Error Notification",
93+
Layout = "${machinename}${newline} ${date} ${newline} ${message} ${newline} ${exception} ${newline}"
94+
};
95+
config.AddTarget("Mail", mailTarget);
96+
97+
//Turn email notification on/off based on the LogEnabled setting
98+
var emailLogLevel = Util.get_setting("ErrorEmailEnabled", "1") == "1" ? LogLevel.Fatal : LogLevel.Off;
99+
config.LoggingRules.Add(new LoggingRule("*", emailLogLevel, mailTarget));
100+
101+
Again, we were able to reuse the existing settings in Web.config to configure NLog.
102+
103+
[View the commit](https://github.com/dpaquette/BugTracker.NET/commit/2a597fda3960aa6a893819f9ce861694c3f29003)
104+
105+
We have now replaced all the custom logging code in BugTracker with a very popular and well supported open source logging framework.

0 commit comments

Comments
 (0)