我想要一些具有特定功能的日志记录机制或框架。我已经在我的应用程序(dll库)中使用
Log.WriteLine("{0}.{1}()", System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType, System.Reflection.MethodInfo.GetCurrentMethod().Name);其中,Log是具有使用Streamwriter写入文件的能力的静态类
public void LogWriteLine(string text, params object[] args) {
lock (this) {
StreamWriter log = new StreamWriter(logFile, true);
if (log != null) {
log.WriteLine("{0:yyyy-MM-dd HH:mm:ss} {1}", DateTime.Now, String.Format(text, args));
log.Flush();
log.Close();
}
}
}我的问题是,我没有让Log.WriteLine调用遍及我的应用程序,只是在应用程序的特定部分,因为它会创建非常大的文件。但现在,我构建了我的应用程序,并将其发布给开发人员,他们为此工作了几天。在那之后,他们给我发送了bug,但在我的版本中,bug不再存在(应用程序的开发仍在继续,所以它们可以被修复)。
因此,我想在应用程序中有一些设置文件,告诉应用程序,我需要更多的日志,并能够运行应用程序的测试人员版本,而不是重建它与记录器的不同设置。
简而言之:我如何从一些设置文件中告诉应用程序只记录一些特定的东西,例如只记录一个类或一个方法?
发布于 2012-02-20 17:53:22
我需要做一件类似的事情。我就是这么做的。
我创建了一个category类,并在初始化日志对象时将其用作参数。
/// <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);“行中看到的,我的日志对象是一个单例对象。
singleton有一些添加和删除类别的方法
/// <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 );
}
}
}
}发布于 2012-02-20 16:57:09
您可以看看.NET跟踪。下面是一个非常简短的介绍(1页):
http://www.codeguru.com/csharp/.net/net_debugging/tracing/article.php/c5919
发布于 2012-02-20 16:59:57
我会看看Log4Net。Log4Net提供日志级别的App.Config配置,重定向到多个输出,允许同步或异步(缓冲)日志记录。
这里有一篇好文章:http://www.codeproject.com/Articles/14819/How-to-use-log4net
我写了一篇关于如何使用正则表达式查找和替换来全面更改整个应用程序的日志记录以使用另一种语法的文章。请参阅this previous answer和this blog article。
https://stackoverflow.com/questions/9358282
复制相似问题