Skip to content

Commit

Permalink
Added ability to specify Elmah log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
dreing1130 authored and phatboyg committed May 3, 2016
1 parent a491122 commit c9beebf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/Topshelf.Elmah/ElmahConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,15 @@ public static void UseElmah(this HostConfigurator configurator)
{
ElmahLogWriterFactory.Use();
}

/// <summary>
/// Specify that you want to use the Elmah logging engine.
/// </summary>
/// <param name="configurator"> </param>
/// <param name="logLevels">The desired level of elmah logging</param>
public static void UseElmah(this HostConfigurator configurator, ElmahLogLevels logLevels)
{
ElmahLogWriterFactory.Use(logLevels);
}
}
}
21 changes: 19 additions & 2 deletions src/Topshelf.Elmah/Logging/ElmahLogWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ private void WriteToElmah(ElmahLogLevelsEnum logLevel, string message, Exception
error.HostName = Environment.MachineName;
error.Detail = exception == null ? message : exception.StackTrace;

_log.Log(error);
if(IsLogLevelEnabled(logLevel))
_log.Log(error);
}

private string GetLogLevel(ElmahLogLevelsEnum logLevel)
Expand Down Expand Up @@ -244,7 +245,23 @@ public void FatalFormat(IFormatProvider provider, string format, params object[]
{
WriteToElmah(ElmahLogLevelsEnum.Fatal, format, args);
}

private bool IsLogLevelEnabled(ElmahLogLevelsEnum logLevel)
{
switch (logLevel)
{
case ElmahLogLevelsEnum.Debug:
return IsDebugEnabled;
case ElmahLogLevelsEnum.Info:
return IsInfoEnabled;
case ElmahLogLevelsEnum.Warn:
return IsWarnEnabled;
case ElmahLogLevelsEnum.Error:
return IsErrorEnabled;
case ElmahLogLevelsEnum.Fatal:
return IsFatalEnabled;
}
return true;
}
public bool IsDebugEnabled
{
get { return _logLevels.IsDebugEnabled; }
Expand Down
20 changes: 16 additions & 4 deletions src/Topshelf.Elmah/Logging/ElmahLogWriterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,41 @@ namespace Topshelf.Logging
public class ElmahLogWriterFactory :
LogWriterFactory
{
private readonly ElmahLogLevels _logLevels;
private ElmahLogWriterFactory(ElmahLogLevels logLevels)
{
_logLevels = logLevels;
}

public LogWriter Get(string name)
{
return new ElmahLogWriter();
return new ElmahLogWriter(_logLevels);
}

public void Shutdown()
{

}

public static void Use()
public static void Use(ElmahLogLevels logLevels = null)
{
HostLogger.UseLogger(new ElmahHostLoggerConfigurator());
HostLogger.UseLogger(new ElmahHostLoggerConfigurator(logLevels));
}


[Serializable]
public class ElmahHostLoggerConfigurator :
HostLoggerConfigurator
{
private readonly ElmahLogLevels _logLevels;
public ElmahHostLoggerConfigurator(ElmahLogLevels logLevels)
{
_logLevels = logLevels;
}

public LogWriterFactory CreateLogWriterFactory()
{
return new ElmahLogWriterFactory();
return new ElmahLogWriterFactory(_logLevels);
}
}
}
Expand Down

0 comments on commit c9beebf

Please sign in to comment.