From e4ffc5602042a209753ac3e113f730ec8990a1aa Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Wed, 18 Jan 2017 15:16:05 -0800 Subject: [PATCH] Fixed logger not sending emails correctly. --- .../Error/Controllers/ErrorController.cs | 51 ++++++++++-- Teknik/Teknik.csproj | 4 + Utilities/Logging/{Logging.cs => Logger.cs} | 77 ++++++++++--------- Utilities/Logging/Logging.csproj | 2 +- 4 files changed, 93 insertions(+), 41 deletions(-) rename Utilities/Logging/{Logging.cs => Logger.cs} (57%) diff --git a/Teknik/Areas/Error/Controllers/ErrorController.cs b/Teknik/Areas/Error/Controllers/ErrorController.cs index 126e295..5aec587 100644 --- a/Teknik/Areas/Error/Controllers/ErrorController.cs +++ b/Teknik/Areas/Error/Controllers/ErrorController.cs @@ -9,6 +9,7 @@ using Teknik.Areas.Error.ViewModels; using Teknik.Controllers; using Teknik.Filters; using Teknik.Utilities; +using Teknik.Logging; namespace Teknik.Areas.Error.Controllers { @@ -27,10 +28,18 @@ namespace Teknik.Areas.Error.Controllers Response.TrySkipIisCustomErrors = true; } + string errorMessage = "General Exception"; + if (Request != null && Request.Url != null) + { + errorMessage += " on page: " + Request.Url.AbsoluteUri; + } + + Logger.WriteEntry(LogLevel.Error, errorMessage, exception); + ErrorViewModel model = new ErrorViewModel(); model.Exception = exception; - return View("/Areas/Error/Views/Error/Exception.cshtml", model); + return View("~/Areas/Error/Views/Error/Exception.cshtml", model); } [TrackPageView] @@ -46,11 +55,19 @@ namespace Teknik.Areas.Error.Controllers Response.TrySkipIisCustomErrors = true; } + string errorMessage = "General HTTP Exception"; + if (Request != null && Request.Url != null) + { + errorMessage += " on page: " + Request.Url.AbsoluteUri; + } + + Logger.WriteEntry(LogLevel.Error, errorMessage, exception); + ErrorViewModel model = new ErrorViewModel(); model.Description = exception.Message; model.Exception = exception; - return View("/Areas/Error/Views/Error/General.cshtml", model); + return View("~/Areas/Error/Views/Error/General.cshtml", model); } [AllowAnonymous] @@ -65,10 +82,18 @@ namespace Teknik.Areas.Error.Controllers Response.TrySkipIisCustomErrors = true; } + string errorMessage = "Access Denied"; + if (Request != null && Request.Url != null) + { + errorMessage += " on page: " + Request.Url.AbsoluteUri; + } + + Logger.WriteEntry(LogLevel.Error, errorMessage, exception); + ErrorViewModel model = new ErrorViewModel(); model.Exception = exception; - return View("/Areas/Error/Views/Error/Http403.cshtml", model); + return View("~/Areas/Error/Views/Error/Http403.cshtml", model); } [AllowAnonymous] @@ -83,10 +108,18 @@ namespace Teknik.Areas.Error.Controllers Response.TrySkipIisCustomErrors = true; } + string errorMessage = "Page Not Found"; + if (Request != null && Request.Url != null) + { + errorMessage += " for page: " + Request.Url.AbsoluteUri; + } + + Logger.WriteEntry(LogLevel.Error, errorMessage, exception); + ErrorViewModel model = new ErrorViewModel(); model.Exception = exception; - return View("/Areas/Error/Views/Error/Http404.cshtml", model); + return View("~/Areas/Error/Views/Error/Http404.cshtml", model); } [TrackPageView] @@ -102,10 +135,18 @@ namespace Teknik.Areas.Error.Controllers Response.TrySkipIisCustomErrors = true; } + string errorMessage = "Server Error"; + if (Request != null && Request.Url != null) + { + errorMessage += " on page: " + Request.Url.AbsoluteUri; + } + + Logger.WriteEntry(LogLevel.Error, errorMessage, exception); + ErrorViewModel model = new ErrorViewModel(); model.Exception = exception; - return View("/Areas/Error/Views/Error/Http500.cshtml", model); + return View("~/Areas/Error/Views/Error/Http500.cshtml", model); } private string GetIPAddress() diff --git a/Teknik/Teknik.csproj b/Teknik/Teknik.csproj index 161589f..3cc62e8 100644 --- a/Teknik/Teknik.csproj +++ b/Teknik/Teknik.csproj @@ -692,6 +692,10 @@ {f0da1b67-af92-4b4a-8669-7e81645ff996} Configuration + + {77e865fd-f08b-4f07-9676-bc2fdcc7244c} + Logging + {c492c2c6-d45a-498b-84a2-6d4c8bf9de77} Piwik diff --git a/Utilities/Logging/Logging.cs b/Utilities/Logging/Logger.cs similarity index 57% rename from Utilities/Logging/Logging.cs rename to Utilities/Logging/Logger.cs index 19ef28e..7e07ba6 100644 --- a/Utilities/Logging/Logging.cs +++ b/Utilities/Logging/Logger.cs @@ -10,8 +10,10 @@ using Teknik.Utilities; namespace Teknik.Logging { - public static class Logging + public static class Logger { + private static object Locker = new object(); + private static Config m_Config { get @@ -61,45 +63,49 @@ namespace Teknik.Logging if (log.Level >= minLogLevel) { - if (!Directory.Exists(m_Config.LoggingConfig.OutputDirectory)) + // Lock the file processing so only 1 thread is working on the log file at a time + lock (Locker) { - Directory.CreateDirectory(m_Config.LoggingConfig.OutputDirectory); - } - // Get current log file - string fileName = Constants.LOG_FILE_NAME_PREFIX + Constants.LOG_FILE_EXT; - string logFile = Path.Combine(m_Config.LoggingConfig.OutputDirectory, fileName); - - if (File.Exists(logFile)) - { - // File already exists, so lets see if we need to rotate it - if (m_Config.LoggingConfig.RotateLogs) + if (!Directory.Exists(m_Config.LoggingConfig.OutputDirectory)) { - FileInfo info = new FileInfo(logFile); - if (m_Config.LoggingConfig.MaxSize < info.Length && m_Config.LoggingConfig.MaxSize > 0) - { - // File is too large, so let's create a new name for it based on todays date - string newFileName = Constants.LOG_FILE_NAME_PREFIX + "_" + DateTime.Now.ToString("yyyyMMdd") + Constants.LOG_FILE_EXT; - newFileName = FileHelper.MakeUniqueFilename(newFileName, m_Config.LoggingConfig.OutputDirectory); - string newLog = Path.Combine(m_Config.LoggingConfig.OutputDirectory, newFileName); + Directory.CreateDirectory(m_Config.LoggingConfig.OutputDirectory); + } + // Get current log file + string fileName = Constants.LOG_FILE_NAME_PREFIX + Constants.LOG_FILE_EXT; + string logFile = Path.Combine(m_Config.LoggingConfig.OutputDirectory, fileName); - // Move the current file to the new file - File.Move(logFile, newLog); - } - - // Make sure we have less than the max number of logs - List totalFiles = Directory.GetFiles(m_Config.LoggingConfig.OutputDirectory, string.Format("{0}*{1}", Constants.LOG_FILE_NAME_PREFIX, Constants.LOG_FILE_EXT), SearchOption.TopDirectoryOnly).ToList(); - if (totalFiles.Count + 1 > m_Config.LoggingConfig.MaxCount && m_Config.LoggingConfig.MaxCount > 0) + if (File.Exists(logFile)) + { + // File already exists, so lets see if we need to rotate it + if (m_Config.LoggingConfig.RotateLogs) { - // We will have too many logs, so let's remove the last one - totalFiles.Sort(); - string fileToRemove = totalFiles[totalFiles.Count - 1]; - File.Delete(fileToRemove); + FileInfo info = new FileInfo(logFile); + if (m_Config.LoggingConfig.MaxSize < info.Length && m_Config.LoggingConfig.MaxSize > 0) + { + // File is too large, so let's create a new name for it based on todays date + string newFileName = Constants.LOG_FILE_NAME_PREFIX + "_" + DateTime.Now.ToString("yyyyMMdd") + Constants.LOG_FILE_EXT; + newFileName = FileHelper.MakeUniqueFilename(newFileName, m_Config.LoggingConfig.OutputDirectory); + string newLog = Path.Combine(m_Config.LoggingConfig.OutputDirectory, newFileName); + + // Move the current file to the new file + File.Move(logFile, newLog); + } + + // Make sure we have less than the max number of logs + List totalFiles = Directory.GetFiles(m_Config.LoggingConfig.OutputDirectory, string.Format("{0}*{1}", Constants.LOG_FILE_NAME_PREFIX, Constants.LOG_FILE_EXT), SearchOption.TopDirectoryOnly).ToList(); + if (totalFiles.Count + 1 > m_Config.LoggingConfig.MaxCount && m_Config.LoggingConfig.MaxCount > 0) + { + // We will have too many logs, so let's remove the last one + totalFiles.Sort(); + string fileToRemove = totalFiles[totalFiles.Count - 1]; + File.Delete(fileToRemove); + } } } - } - // We have rotated if needed, so let's write the entry - File.AppendAllText(logFile, log.ToString()); + // We have rotated if needed, so let's write the entry + File.AppendAllText(logFile, log.ToString() + Environment.NewLine); + } } // Send Email Message if enabled @@ -110,7 +116,7 @@ namespace Teknik.Logging Enum.TryParse(m_Config.LoggingConfig.EmailLevel, out minEmailLevel); if (log.Level >= minEmailLevel) { - string subject = string.Format("{0} Log Message"); + string subject = string.Format("{0} Log Message", log.Level); string message = "Message: " + log.Message; if (log.Exception != null) { @@ -144,7 +150,8 @@ namespace Teknik.Logging client.Send(mail); } - catch (Exception) { /* don't handle something in the handler */ } + catch (Exception ex) { /* don't handle something in the handler */ + } } } } diff --git a/Utilities/Logging/Logging.csproj b/Utilities/Logging/Logging.csproj index e26accb..11403f9 100644 --- a/Utilities/Logging/Logging.csproj +++ b/Utilities/Logging/Logging.csproj @@ -43,7 +43,7 @@ - +