nicko 2004/09/10 13:58:38
Modified: src/Appender ColoredConsoleAppender.cs EventLogAppender.cs
Added: src/Util LevelMapping.cs LevelMappingEntry.cs
Log:
Added LevelMapping helper to simplify writing appender configurable level
mappings, e.g. the Level to Color mapping of the ColoredConsoleAppender.
Updated the ColoredConsoleAppender and EventLogAppender to use the
LevelMapping.
Revision Changes Path
1.9 +80 -59 logging-log4net/src/Appender/ColoredConsoleAppender.cs
Index: ColoredConsoleAppender.cs
===================================================================
RCS file: /home/cvs/logging-log4net/src/Appender/ColoredConsoleAppender.cs,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ColoredConsoleAppender.cs 2 Aug 2004 09:41:41 -0000 1.8
+++ ColoredConsoleAppender.cs 10 Sep 2004 20:58:38 -0000 1.9
@@ -33,6 +33,7 @@
using System.Runtime.InteropServices;
using log4net.Layout;
+using log4net.Util;
namespace log4net.Appender
{
@@ -142,7 +143,7 @@
HighIntensity = 0x0008,
}
- #endregion
+ #endregion // Colors Enum
#region Public Instance Constructors
@@ -225,57 +226,7 @@
/// <param name="mapping">The mapping to add</param>
public void AddMapping(LevelColors mapping)
{
- AddMapping(mapping.Level, mapping.ForeColor,
mapping.BackColor);
- }
-
- /// <summary>
- /// Add a mapping of level to color
- /// </summary>
- /// <param name="level">The level to map to a color</param>
- /// <param name="foreColor">The mapped foreground color for the
specified level</param>
- /// <param name="backColor">The mapped background color for the
specified level</param>
- public void AddMapping(log4net.Core.Level level, Colors
foreColor, Colors backColor)
- {
- ushort usMapping = (ushort)((int)foreColor +
(((int)backColor) << 4) );
- m_Level2ColorMap[level] = usMapping;
- }
-
- /// <summary>
- /// A class to act as a mapping between the level that a
logging call is made at and
- /// the color it should be displayed as.
- /// </summary>
- public class LevelColors
- {
- private log4net.Core.Level m_level;
- private Colors m_foreColor;
- private Colors m_backColor;
-
- /// <summary>
- /// The level to map to a color
- /// </summary>
- public log4net.Core.Level Level
- {
- get { return m_level; }
- set { m_level = value; }
- }
-
- /// <summary>
- /// The mapped foreground color for the specified level
- /// </summary>
- public Colors ForeColor
- {
- get { return m_foreColor; }
- set { m_foreColor = value; }
- }
-
- /// <summary>
- /// The mapped background color for the specified level
- /// </summary>
- public Colors BackColor
- {
- get { return m_backColor; }
- set { m_backColor = value; }
- }
+ m_levelMapping.Add(mapping);
}
#endregion // Public Instance Properties
@@ -310,13 +261,13 @@
// set the output parameters
// Default to white on black
- ushort colorInfo = (UInt16)(Colors.Red | Colors.Blue |
Colors.Green);
+ ushort colorInfo = (ushort)Colors.White;
- // see if there is a lookup.
- Object colLookup = m_Level2ColorMap[loggingEvent.Level];
- if(colLookup != null)
+ // see if there is a specified lookup.
+ LevelColors levelColors =
m_levelMapping.Lookup(loggingEvent.Level) as LevelColors;
+ if (levelColors != null)
{
- colorInfo = (ushort)colLookup;
+ colorInfo = levelColors.CombinedColor;
}
// get the current console color.
@@ -349,6 +300,20 @@
get { return true; }
}
+ /// <summary>
+ /// Initialise the options for this appender
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// Initialise the level to color mappings set on this appender.
+ /// </para>
+ /// </remarks>
+ public override void ActivateOptions()
+ {
+ base.ActivateOptions();
+ m_levelMapping.ActivateOptions();
+ }
+
#endregion // Override implementation of AppenderSkeleton
#region Public Static Fields
@@ -377,7 +342,7 @@
/// <summary>
/// Mapping from level object to color value
/// </summary>
- private System.Collections.Hashtable m_Level2ColorMap = new
System.Collections.Hashtable();
+ private LevelMapping m_levelMapping = new LevelMapping();
#endregion // Private Instances Fields
@@ -435,7 +400,63 @@
public COORD dwMaximumWindowSize;
}
- #endregion
+ #endregion // Win32 Methods
+
+ #region LevelColors LevelMapping Entry
+
+ /// <summary>
+ /// A class to act as a mapping between the level that a
logging call is made at and
+ /// the color it should be displayed as.
+ /// </summary>
+ public class LevelColors : LevelMappingEntry
+ {
+ private Colors m_foreColor;
+ private Colors m_backColor;
+ private ushort m_combinedColor = 0;
+
+ /// <summary>
+ /// The mapped foreground color for the specified level
+ /// </summary>
+ public Colors ForeColor
+ {
+ get { return m_foreColor; }
+ set { m_foreColor = value; }
+ }
+
+ /// <summary>
+ /// The mapped background color for the specified level
+ /// </summary>
+ public Colors BackColor
+ {
+ get { return m_backColor; }
+ set { m_backColor = value; }
+ }
+
+ /// <summary>
+ /// Initialise the options for the object
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// Combine the <see cref="ForeColor"/> and <see
cref="BackColor"/> together.
+ /// </para>
+ /// </remarks>
+ public override void ActivateOptions()
+ {
+ base.ActivateOptions();
+ m_combinedColor = (ushort)( (int)m_foreColor +
(((int)m_backColor) << 4) );
+ }
+
+ /// <summary>
+ /// The combined <see cref="ForeColor"/> and <see
cref="BackColor"/> suitable for
+ /// setting the console color.
+ /// </summary>
+ internal ushort CombinedColor
+ {
+ get { return m_combinedColor; }
+ }
+ }
+
+ #endregion // LevelColors LevelMapping Entry
}
}
1.10 +78 -7 logging-log4net/src/Appender/EventLogAppender.cs
Index: EventLogAppender.cs
===================================================================
RCS file: /home/cvs/logging-log4net/src/Appender/EventLogAppender.cs,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- EventLogAppender.cs 9 Sep 2004 21:53:13 -0000 1.9
+++ EventLogAppender.cs 10 Sep 2004 20:58:38 -0000 1.10
@@ -18,11 +18,11 @@
// MONO 1.0 Beta mcs does not like #if !A && !B && !C syntax
-// .NET Compact Framework 1.0 has no support for System.Diagnostics.EventLog
+// .NET Compact Framework 1.0 has no support for EventLog
#if !NETCF
-// .Mono 1.0 has no support for System.Diagnostics.EventLog
+// .Mono 1.0 has no support for EventLog
#if !MONO
-// SSCLI 1.0 has no support for System.Diagnostics.EventLog
+// SSCLI 1.0 has no support for EventLog
#if !SSCLI
// We don't want framework or platform specific code in the Core version of
// log4net
@@ -50,6 +50,29 @@
/// <para>
/// There is a limit of 32K characters for an event log message
/// </para>
+ /// <para>
+ /// When configuring the EventLogAppender a mapping can be
+ /// specified to map a logging level to an event log entry type. For
example:
+ /// </para>
+ /// <code>
+ /// <mapping>
+ /// <level value="ERROR" />
+ /// <eventLogEntryType value="Error" />
+ /// </mapping>
+ /// <mapping>
+ /// <level value="DEBUG" />
+ /// <eventLogEntryType value="Information" />
+ /// </mapping>
+ /// </code>
+ /// <para>
+ /// The Level is the standard log4net logging level and
eventLogEntryType can be any value
+ /// from the <see cref="EventLogEntryType"/> enum, i.e.:
+ /// <list type="bullet">
+ /// <item><term>Error</term><description>an error
event</description></item>
+ /// <item><term>Warning</term><description>a warning
event</description></item>
+ /// <item><term>Information</term><description>an informational
event</description></item>
+ /// </list>
+ /// </para>
/// </remarks>
/// <author>Aspi Havewala</author>
/// <author>Douglas de la Torre</author>
@@ -144,6 +167,15 @@
set { /* Currently we do not allow the machine name to
be changed */; }
}
+ /// <summary>
+ /// Add a mapping of level to <see cref="EventLogEntryType"/> -
done by the config file
+ /// </summary>
+ /// <param name="mapping">The mapping to add</param>
+ public void AddMapping(Level2EventLogEntryType mapping)
+ {
+ m_levelMapping.Add(mapping);
+ }
+
#endregion // Public Instance Properties
#region Implementation of IOptionHandler
@@ -187,6 +219,8 @@
EventLog.CreateEventSource(m_applicationName,
m_logName, m_machineName);
}
+ m_levelMapping.ActivateOptions();
+
LogLog.Debug("EventLogAppender: Source [" +
m_applicationName + "] is registered to log [" +
EventLog.LogNameFromSourceName(m_applicationName, m_machineName) + "]");
}
@@ -282,18 +316,28 @@
/// <see cref="Level"/> this is a one way mapping. There is
/// a loss of information during the conversion.
/// </remarks>
- virtual protected System.Diagnostics.EventLogEntryType
GetEntryType(Level level)
+ virtual protected EventLogEntryType GetEntryType(Level level)
{
+ // see if there is a specified lookup.
+ Level2EventLogEntryType entryType =
m_levelMapping.Lookup(level) as Level2EventLogEntryType;
+ if (entryType != null)
+ {
+ return entryType.EventLogEntryType;
+ }
+
+ // Use default behaviour
+
if (level >= Level.Error)
{
- return
System.Diagnostics.EventLogEntryType.Error;
+ return EventLogEntryType.Error;
}
else if (level == Level.Warn)
{
- return
System.Diagnostics.EventLogEntryType.Warning;
+ return EventLogEntryType.Warning;
}
+
// Default setting
- return System.Diagnostics.EventLogEntryType.Information;
+ return EventLogEntryType.Information;
}
#endregion // Protected Instance Methods
@@ -318,7 +362,34 @@
/// </summary>
private string m_machineName;
+ /// <summary>
+ /// Mapping from level object to EventLogEntryType
+ /// </summary>
+ private LevelMapping m_levelMapping = new LevelMapping();
+
#endregion // Private Instance Fields
+
+ #region Level2EventLogEntryType LevelMapping Entry
+
+ /// <summary>
+ /// A class to act as a mapping between the level that a
logging call is made at and
+ /// the color it should be displayed as.
+ /// </summary>
+ public class Level2EventLogEntryType : LevelMappingEntry
+ {
+ private EventLogEntryType m_entryType;
+
+ /// <summary>
+ /// The <see cref="EventLogEntryType"/> for this entry
+ /// </summary>
+ public EventLogEntryType EventLogEntryType
+ {
+ get { return m_entryType; }
+ set { m_entryType = value; }
+ }
+ }
+
+ #endregion // LevelColors LevelMapping Entry
}
}
1.1 logging-log4net/src/Util/LevelMapping.cs
Index: LevelMapping.cs
===================================================================
#region Copyright & License
//
// Copyright 2001-2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using System;
using System.Collections;
using log4net.Core;
namespace log4net.Util
{
/// <summary>
/// Manages a mapping from levels to <see cref="LevelMappingEntry"/>
/// </summary>
/// <author>Nicko Cadell</author>
public sealed class LevelMapping : IOptionHandler
{
#region Public Instance Constructors
/// <summary>
/// Default constructor
/// </summary>
public LevelMapping()
{
}
#endregion // Public Instance Constructors
#region Public Instance Methods
/// <summary>
/// Add a <see cref="LevelMappingEntry"/> to this mapping
/// </summary>
/// <param name="entry">the entry to add</param>
/// <remarks>
/// <para>
/// If a <see cref="LevelMappingEntry"/> has previously been
added
/// for the same <see cref="Level"/> then that entry will be
/// overwritten.
/// </para>
/// </remarks>
public void Add(LevelMappingEntry entry)
{
if (m_entriesList.ContainsKey(entry.Level))
{
m_entriesList.Remove(entry.Level);
}
m_entriesList.Add(entry.Level, entry);
}
/// <summary>
/// Lookup the mapping for the specified level
/// </summary>
/// <param name="level">the level to lookup</param>
/// <returns>the <see cref="LevelMappingEntry"/> for the level
or <c>null</c> if no mapping found</returns>
public LevelMappingEntry Lookup(Level level)
{
foreach(LevelMappingEntry entry in m_entries)
{
if (level >= entry.Level)
{
return entry;
}
}
return null;
}
#endregion // Public Instance Methods
#region IOptionHandler Members
/// <summary>
/// Initialize options
/// </summary>
/// <remarks>
/// <para>
/// Caches the sorted list of <see cref="LevelMappingEntry"/>
in an array
/// </para>
/// </remarks>
public void ActivateOptions()
{
m_entries = new LevelMappingEntry[m_entriesList.Count];
m_entriesList.GetValueList().CopyTo(m_entries, 0);
Array.Reverse(m_entries);
foreach(LevelMappingEntry entry in m_entries)
{
entry.ActivateOptions();
}
}
#endregion // IOptionHandler Members
#region Private Instance Fields
private SortedList m_entriesList = new SortedList();
private LevelMappingEntry[] m_entries = null;
#endregion // Private Instance Fields
}
}
1.1 logging-log4net/src/Util/LevelMappingEntry.cs
Index: LevelMappingEntry.cs
===================================================================
#region Copyright & License
//
// Copyright 2001-2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using System;
using System.Collections;
using log4net.Core;
namespace log4net.Util
{
/// <summary>
/// An entry in the <see cref="LevelMapping"/>
/// </summary>
/// <remarks>
/// <para>
/// This is an abstract base class for types that are stored in the
/// <see cref="LevelMapping"/> object.
/// </para>
/// </remarks>
/// <author>Nicko Cadell</author>
public abstract class LevelMappingEntry : IOptionHandler
{
#region Public Instance Constructors
/// <summary>
/// Default protected constructor
/// </summary>
protected LevelMappingEntry()
{
}
#endregion // Public Instance Constructors
#region Public Instance Properties
/// <summary>
/// The level that is the key for this mapping
/// </summary>
public Level Level
{
get { return m_level; }
set { m_level = value; }
}
#endregion // Public Instance Properties
#region IOptionHandler Members
/// <summary>
/// Initialize any options defined on this entry
/// </summary>
/// <remarks>
/// <para>
/// Should be overridden by any classes that need to initialise
based on their options
/// </para>
/// </remarks>
virtual public void ActivateOptions()
{
// default implementation is to do nothing
}
#endregion // IOptionHandler Members
#region Private Instance Fields
private Level m_level;
#endregion // Private Instance Fields
}
}