Browse Source

Fixed logger not sending emails correctly.

tags/2.0.6
Teknikode 3 years ago
parent
commit
e4ffc56020

+ 46
- 5
Teknik/Areas/Error/Controllers/ErrorController.cs View File

@@ -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()

+ 4
- 0
Teknik/Teknik.csproj View File

@@ -692,6 +692,10 @@
<Project>{f0da1b67-af92-4b4a-8669-7e81645ff996}</Project>
<Name>Configuration</Name>
</ProjectReference>
<ProjectReference Include="..\Utilities\Logging\Logging.csproj">
<Project>{77e865fd-f08b-4f07-9676-bc2fdcc7244c}</Project>
<Name>Logging</Name>
</ProjectReference>
<ProjectReference Include="..\Utilities\Piwik\Piwik.csproj">
<Project>{c492c2c6-d45a-498b-84a2-6d4c8bf9de77}</Project>
<Name>Piwik</Name>

Utilities/Logging/Logging.cs → Utilities/Logging/Logger.cs View File

@@ -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))
{
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))
// Lock the file processing so only 1 thread is working on the log file at a time
lock (Locker)
{
// 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);

// Move the current file to the new file
File.Move(logFile, newLog);
}
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);

// Make sure we have less than the max number of logs
List<string> 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<string> 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 */
}
}
}
}

+ 1
- 1
Utilities/Logging/Logging.csproj View File

@@ -43,7 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Logging.cs" />
<Compile Include="Logger.cs" />
<Compile Include="LogLevel.cs" />
<Compile Include="LogMessage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

Loading…
Cancel
Save