Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add debugEnabled option to InMemoryLog and EventLogger
  • Loading branch information
mythz committed Apr 22, 2015
1 parent dd8e6c1 commit a6321ba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
24 changes: 15 additions & 9 deletions src/ServiceStack.Common/Support/InMemoryLogFactory.cs
Expand Up @@ -14,14 +14,20 @@ namespace ServiceStack.Support
public class InMemoryLogFactory
: ILogFactory
{
private readonly bool debugEnabled;
public InMemoryLogFactory(bool debugEnabled = false)
{
this.debugEnabled = debugEnabled;
}

public ILog GetLogger(Type type)
{
return new InMemoryLog(type.Name);
return new InMemoryLog(type.Name) { IsDebugEnabled = debugEnabled };
}

public ILog GetLogger(string typeName)
{
return new InMemoryLog(typeName);
return new InMemoryLog(typeName) { IsDebugEnabled = debugEnabled };
}
}

Expand Down Expand Up @@ -110,17 +116,20 @@ private void AppendToLog(ICollection<string> logEntries, string message)

public void Debug(object message)
{
AppendToLog(DebugEntries, message);
if (IsDebugEnabled)
AppendToLog(DebugEntries, message);
}

public void Debug(object message, Exception exception)
{
AppendToLog(DebugEntries, DebugExceptions, message, exception);
if (IsDebugEnabled)
AppendToLog(DebugEntries, DebugExceptions, message, exception);
}

public void DebugFormat(string format, params object[] args)
{
AppendToLog(DebugEntries, format, args);
if (IsDebugEnabled)
AppendToLog(DebugEntries, format, args);
}

public void Error(object message)
Expand Down Expand Up @@ -183,9 +192,6 @@ public void WarnFormat(string format, params object[] args)
AppendToLog(WarnEntries, format, args);
}

public bool IsDebugEnabled
{
get { return true; }
}
public bool IsDebugEnabled { get; set; }
}
}
16 changes: 8 additions & 8 deletions src/ServiceStack.Logging.EventLog/EventLogFactory.cs
Expand Up @@ -9,22 +9,19 @@ public class EventLogFactory : ILogFactory
{
private readonly string eventLogName;
private readonly string eventLogSource;

/// <summary>
/// Initializes a new instance of the <see cref="EventLogFactory"/> class.
/// </summary>
/// <param name="eventLogName">Name of the event log.</param>
public EventLogFactory(string eventLogName) : this(eventLogName, null) { }
private readonly bool debugEnabled;

/// <summary>
/// Initializes a new instance of the <see cref="EventLogFactory"/> class.
/// </summary>
/// <param name="eventLogName">Name of the event log. Default is 'ServiceStack.Logging.EventLog'</param>
/// <param name="eventLogSource">The event log source. Default is 'Application'</param>
public EventLogFactory(string eventLogName, string eventLogSource)
/// <param name="debugEnabled">Whether to write EventLog entries for DEBUG logs</param>
public EventLogFactory(string eventLogName = null, string eventLogSource = null, bool debugEnabled = false)
{
this.eventLogName = eventLogName ?? "ServiceStack.Logging.EventLog";
this.eventLogSource = eventLogSource ?? "Application";
this.debugEnabled = debugEnabled;
}

/// <summary>
Expand All @@ -44,7 +41,10 @@ public ILog GetLogger(Type type)
/// <returns></returns>
public ILog GetLogger(string typeName)
{
return new EventLogger(eventLogName, eventLogSource);
return new EventLogger(eventLogName, eventLogSource)
{
IsDebugEnabled = debugEnabled
};
}
}
}
11 changes: 7 additions & 4 deletions src/ServiceStack.Logging.EventLog/EventLogger.cs
Expand Up @@ -34,7 +34,7 @@ public EventLogger(string eventLogName, string eventLogSource)
this.eventLogSource = eventLogSource;
}

public bool IsDebugEnabled { get { return true; } }
public bool IsDebugEnabled { get; set; }

/// <summary>
/// Writes the specified message.
Expand Down Expand Up @@ -84,7 +84,8 @@ private void Write(object message, Exception execption, EventLogEntryType eventL
/// <param name="message">The message.</param>
public void Debug(object message)
{
Write("DEBUG: " + message, EventLogEntryType.Information);
if (IsDebugEnabled)
Write("DEBUG: " + message, EventLogEntryType.Information);
}

/// <summary>
Expand All @@ -94,7 +95,8 @@ public void Debug(object message)
/// <param name="exception">The exception.</param>
public void Debug(object message, Exception exception)
{
Write("DEBUG: " + message, exception, EventLogEntryType.Information);
if (IsDebugEnabled)
Write("DEBUG: " + message, exception, EventLogEntryType.Information);
}

/// <summary>
Expand All @@ -104,7 +106,8 @@ public void Debug(object message, Exception exception)
/// <param name="args">The args.</param>
public void DebugFormat(string format, params object[] args)
{
Write("DEBUG: " + string.Format(format, args), EventLogEntryType.Information);
if (IsDebugEnabled)
Write("DEBUG: " + string.Format(format, args), EventLogEntryType.Information);
}

/// <summary>
Expand Down

0 comments on commit a6321ba

Please sign in to comment.