我需要做类似的事情。我就是这样做的。
我创建了一个类别类,并在初始化日志对象时将其用作参数。
/// <summary>
/// Category object for logging
/// </summary>
public class Category
{
#region Private Members
private bool m_active;
private string m_name;
private bool m_excludeFromLogFile = false;
#endregion
/// <summary>
/// Create a category and add it to the Logging category list
/// </summary>
/// <param name="name">The Name of the category</param>
/// <param name="active">The active state of the category</param>
/// <param name="exclude">If true any messages for this category will not be written to the log file</param>
/// <param name="addedToList">If true then the new category will be added to the logging category list</param>
public Category(string name, bool active, bool exclude, bool addedToList)
{
m_name = name;
m_active = active;
m_excludeFromLogFile = exclude;
if(addedToList)
{
Log.GetInstance().AddCategory(this);
}
}
#region Public Accessor Methods
// .. Add accessors as required
#endregion
}
从“Log.GetInstance().AddCategory(this);”行可以看出,我的日志记录对象是一个单例。
单例,有一些添加和删除类别的方法
/// <summary>
/// Add a new category to the list of available categories
/// </summary>
/// <param name="newCat">The category object to add</param>
public void AddCategory( Category newCat )
{
// Ensure that the category doesn't already exist in the list
if( this.m_CategoryList.Contains( newCat ) == false )
{
// Add the new category to the list
this.m_CategoryList.Add( newCat );
}
}
/// <summary>
/// Remove a category to the list of available categories
/// </summary>
/// <param name="catName">The name of the category to be removed</param>
public void RemoveCategory( string catName )
{
Category toRemove = null;
// Iterate through the categories looking for a match
foreach( Category cat in this.m_CategoryList)
{
// Compare the category names (case insensitive)
if( cat.Name.ToUpper() == catName.ToUpper() )
{
// Assign the category to remove to a local variable and exit the loop
toRemove = cat;
break;
}
}
// Remove the category if it's been located
if( toRemove != null )
{
this.m_CategoryList.Remove( toRemove );
}
}
在处理日志事件时,现在只需检查类别的活动状态以查看是否需要该消息。
/// <summary>
/// Create a log entry in the log file and then Fire an event for the log message to be handled
/// </summary>
/// <param name="category">The category to log the message against</param>
/// <param name="args"> Message logging arguments used by the event</param>
public void WriteLine(Category category, MessageEventArgs args)
{
// Ensure that the category specified exists in the array list
if( this.m_CategoryList.Contains( category ) )
{
// Ensure the category is active
if(category.Active == true)
{
if(!category.ExcludeFromLogFile)
{
// Try and log the message to the log file
this.WriteLineToFile( category, args );
}
// Ensure an event handler has been assigned
if(MessageEvent != null)
{
// This message event is handled by the UI thread for updating screen components.
MessageEvent(category, args);
}
}
}
}
最后,如果您希望消息显示在屏幕上,您需要在 UI 线程中处理消息事件。这是我的列表视图组件之一的示例...
private void ListViewLogging_MessageEvent(Category category, MessageEventArgs args)
{
// Ensure the event was received in the UI thread
if(this.InvokeRequired)
{
if(args.Message != null)
{
// We aren't in the UI thread so reFire the event using the main thread
this.BeginInvoke(new MessageReceivedDelegate(this.ListViewLogging_MessageEvent), new object[]{category,args});
}
}
else
{
// We are currently in the main thread.
// Lock so no other thread can be handled until event processing has been finished
lock(this)
{
// Create a new ListView item for the new message
ListViewItem newEntry = null;;
// Determine the category type
switch( category.Name )
{
case "Serious":
{
// Serious error detected
if( args.Message.Length > 0 )
{
newEntry = new ListViewItem( new string[]{category.Name, args.Occurred.ToLongTimeString(), args.Message} );
newEntry.BackColor = Color.Red;
}
break;
}
case "Warning":
{
// Warning detected.
if( args.Message.Length > 0 )
{
newEntry = new ListViewItem( new string[]{category.Name, args.Occurred.ToLongTimeString(), args.Message} );
newEntry.BackColor = Color.Orange;
}
break;
}
case "Progress":
{
// If a message has been specified, log it
if( args.Message.Length > 0 )
{
newEntry = new ListViewItem( new string[]{"", args.Occurred.ToLongTimeString(), args.Message} );
}
break;
}
case "Debug":
{
// Just a standard Debug event so just display the text on the screen
if( args.Message.Length > 0 )
{
newEntry = new ListViewItem( new string[]{category.Name, args.Occurred.ToLongTimeString(), args.Message} );
newEntry.BackColor = Color.LightGreen;
}
break;
}
case "Info":
default:
{
// Just a standard event so just display the text on the screen
if( args.Message.Length > 0 )
{
newEntry = new ListViewItem( new string[]{category.Name, args.Occurred.ToLongTimeString(), args.Message} );
}
break;
}
}
// Add the item if it's been populated
if( newEntry != null )
{
this.Items.Add( newEntry );
this.EnsureVisible( this.Items.Count-1 );
}
}
}
}