mirror of
https://git.teknik.io/Teknikode/Teknik.git
synced 2023-08-02 14:16:22 +02:00
Fixed logger not sending emails correctly.
This commit is contained in:
parent
fc08678d7d
commit
e4ffc56020
@ -9,6 +9,7 @@ using Teknik.Areas.Error.ViewModels;
|
|||||||
using Teknik.Controllers;
|
using Teknik.Controllers;
|
||||||
using Teknik.Filters;
|
using Teknik.Filters;
|
||||||
using Teknik.Utilities;
|
using Teknik.Utilities;
|
||||||
|
using Teknik.Logging;
|
||||||
|
|
||||||
namespace Teknik.Areas.Error.Controllers
|
namespace Teknik.Areas.Error.Controllers
|
||||||
{
|
{
|
||||||
@ -27,10 +28,18 @@ namespace Teknik.Areas.Error.Controllers
|
|||||||
Response.TrySkipIisCustomErrors = true;
|
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();
|
ErrorViewModel model = new ErrorViewModel();
|
||||||
model.Exception = exception;
|
model.Exception = exception;
|
||||||
|
|
||||||
return View("/Areas/Error/Views/Error/Exception.cshtml", model);
|
return View("~/Areas/Error/Views/Error/Exception.cshtml", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TrackPageView]
|
[TrackPageView]
|
||||||
@ -46,11 +55,19 @@ namespace Teknik.Areas.Error.Controllers
|
|||||||
Response.TrySkipIisCustomErrors = true;
|
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();
|
ErrorViewModel model = new ErrorViewModel();
|
||||||
model.Description = exception.Message;
|
model.Description = exception.Message;
|
||||||
model.Exception = exception;
|
model.Exception = exception;
|
||||||
|
|
||||||
return View("/Areas/Error/Views/Error/General.cshtml", model);
|
return View("~/Areas/Error/Views/Error/General.cshtml", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
@ -65,10 +82,18 @@ namespace Teknik.Areas.Error.Controllers
|
|||||||
Response.TrySkipIisCustomErrors = true;
|
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();
|
ErrorViewModel model = new ErrorViewModel();
|
||||||
model.Exception = exception;
|
model.Exception = exception;
|
||||||
|
|
||||||
return View("/Areas/Error/Views/Error/Http403.cshtml", model);
|
return View("~/Areas/Error/Views/Error/Http403.cshtml", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
@ -83,10 +108,18 @@ namespace Teknik.Areas.Error.Controllers
|
|||||||
Response.TrySkipIisCustomErrors = true;
|
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();
|
ErrorViewModel model = new ErrorViewModel();
|
||||||
model.Exception = exception;
|
model.Exception = exception;
|
||||||
|
|
||||||
return View("/Areas/Error/Views/Error/Http404.cshtml", model);
|
return View("~/Areas/Error/Views/Error/Http404.cshtml", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TrackPageView]
|
[TrackPageView]
|
||||||
@ -102,10 +135,18 @@ namespace Teknik.Areas.Error.Controllers
|
|||||||
Response.TrySkipIisCustomErrors = true;
|
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();
|
ErrorViewModel model = new ErrorViewModel();
|
||||||
model.Exception = exception;
|
model.Exception = exception;
|
||||||
|
|
||||||
return View("/Areas/Error/Views/Error/Http500.cshtml", model);
|
return View("~/Areas/Error/Views/Error/Http500.cshtml", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetIPAddress()
|
private string GetIPAddress()
|
||||||
|
@ -692,6 +692,10 @@
|
|||||||
<Project>{f0da1b67-af92-4b4a-8669-7e81645ff996}</Project>
|
<Project>{f0da1b67-af92-4b4a-8669-7e81645ff996}</Project>
|
||||||
<Name>Configuration</Name>
|
<Name>Configuration</Name>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
|
<ProjectReference Include="..\Utilities\Logging\Logging.csproj">
|
||||||
|
<Project>{77e865fd-f08b-4f07-9676-bc2fdcc7244c}</Project>
|
||||||
|
<Name>Logging</Name>
|
||||||
|
</ProjectReference>
|
||||||
<ProjectReference Include="..\Utilities\Piwik\Piwik.csproj">
|
<ProjectReference Include="..\Utilities\Piwik\Piwik.csproj">
|
||||||
<Project>{c492c2c6-d45a-498b-84a2-6d4c8bf9de77}</Project>
|
<Project>{c492c2c6-d45a-498b-84a2-6d4c8bf9de77}</Project>
|
||||||
<Name>Piwik</Name>
|
<Name>Piwik</Name>
|
||||||
|
@ -10,8 +10,10 @@ using Teknik.Utilities;
|
|||||||
|
|
||||||
namespace Teknik.Logging
|
namespace Teknik.Logging
|
||||||
{
|
{
|
||||||
public static class Logging
|
public static class Logger
|
||||||
{
|
{
|
||||||
|
private static object Locker = new object();
|
||||||
|
|
||||||
private static Config m_Config
|
private static Config m_Config
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@ -61,45 +63,49 @@ namespace Teknik.Logging
|
|||||||
|
|
||||||
if (log.Level >= minLogLevel)
|
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);
|
if (!Directory.Exists(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)
|
|
||||||
{
|
{
|
||||||
FileInfo info = new FileInfo(logFile);
|
Directory.CreateDirectory(m_Config.LoggingConfig.OutputDirectory);
|
||||||
if (m_Config.LoggingConfig.MaxSize < info.Length && m_Config.LoggingConfig.MaxSize > 0)
|
}
|
||||||
{
|
// Get current log file
|
||||||
// File is too large, so let's create a new name for it based on todays date
|
string fileName = Constants.LOG_FILE_NAME_PREFIX + Constants.LOG_FILE_EXT;
|
||||||
string newFileName = Constants.LOG_FILE_NAME_PREFIX + "_" + DateTime.Now.ToString("yyyyMMdd") + Constants.LOG_FILE_EXT;
|
string logFile = Path.Combine(m_Config.LoggingConfig.OutputDirectory, fileName);
|
||||||
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
|
if (File.Exists(logFile))
|
||||||
File.Move(logFile, newLog);
|
{
|
||||||
}
|
// File already exists, so lets see if we need to rotate it
|
||||||
|
if (m_Config.LoggingConfig.RotateLogs)
|
||||||
// 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
|
FileInfo info = new FileInfo(logFile);
|
||||||
totalFiles.Sort();
|
if (m_Config.LoggingConfig.MaxSize < info.Length && m_Config.LoggingConfig.MaxSize > 0)
|
||||||
string fileToRemove = totalFiles[totalFiles.Count - 1];
|
{
|
||||||
File.Delete(fileToRemove);
|
// 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
|
// We have rotated if needed, so let's write the entry
|
||||||
File.AppendAllText(logFile, log.ToString());
|
File.AppendAllText(logFile, log.ToString() + Environment.NewLine);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send Email Message if enabled
|
// Send Email Message if enabled
|
||||||
@ -110,7 +116,7 @@ namespace Teknik.Logging
|
|||||||
Enum.TryParse(m_Config.LoggingConfig.EmailLevel, out minEmailLevel);
|
Enum.TryParse(m_Config.LoggingConfig.EmailLevel, out minEmailLevel);
|
||||||
if (log.Level >= 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;
|
string message = "Message: " + log.Message;
|
||||||
if (log.Exception != null)
|
if (log.Exception != null)
|
||||||
{
|
{
|
||||||
@ -144,7 +150,8 @@ namespace Teknik.Logging
|
|||||||
|
|
||||||
client.Send(mail);
|
client.Send(mail);
|
||||||
}
|
}
|
||||||
catch (Exception) { /* don't handle something in the handler */ }
|
catch (Exception ex) { /* don't handle something in the handler */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -43,7 +43,7 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Logging.cs" />
|
<Compile Include="Logger.cs" />
|
||||||
<Compile Include="LogLevel.cs" />
|
<Compile Include="LogLevel.cs" />
|
||||||
<Compile Include="LogMessage.cs" />
|
<Compile Include="LogMessage.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user