2012-02-20 92 views
2

我想问一些具有特定功能的日志记录机制或框架。我已经在我的应用程序(DLL库)与动态日志框架或技术

Log.WriteLine("{0}.{1}()", System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType, System.Reflection.MethodInfo.GetCurrentMethod().Name); 

在日志是静态类,其功能洛编写使用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只需在应用程序的特定部分调用我的应用程序,因为它会创建非常大的文件。但现在,我构建了我的应用程序,并将其发布给开发人员,他们为此付出了一些时间。之后,他们发给我错误,但在我的版本中,错误已不再存在(应用程序的开发继续进行,因此可以修复)。

因此,我想在应用程序中设置一些设置文件,告诉应用程序我需要更多的日志,并且能够运行应用程序的测试版本,而无需使用记录器的不同设置重新构建它。

简而言之:我怎样才能告诉应用程序窗体一些设置文件,只记录特定的东西,例如只有一个类或一种方法?

回答

1

我需要做类似的事情。这是我做到的。

我创建了一个类别类,并在日志记录对象初始化时用作参数。你可以看到“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); 
      } 
     } 
    } 
} 
+0

好吧,知道如何激活或停用特定事件的日志记录而不更改和重建代码? – Zavael 2012-02-20 10:20:06

+0

您需要访问器方法来记录对象中的类别容器。找到类别并设置活动状态。另一种方法是在默认情况下使用一些静态类别(我使用严重,警告,信息和调试),然后对这些特定类别使用访问方法。 – TeamWild 2012-02-20 10:54:46

+0

首先我想了解一些机制,在设置文件中,我可以动态地添加类名或方法名,然后运行coul读取设置文件(在开始时)并决定要记录什么的应用程序......但这种“分层“的日志调试,警告等也可以是有益的,谢谢:) – Zavael 2012-02-20 12:16:06

1

您可以使用log4net的记录的和过滤的。见introduction article

+0

+1感谢答案,我会看它太 – Zavael 2012-02-20 12:17:28

2

我会看看Log4Net。 Log4Net提供日志级别的App.Config配置,重定向到多个输出,允许同步或异步(缓冲)日志记录。

一个很好的文章在这里:http://www.codeproject.com/Articles/14819/How-to-use-log4net

我写了一篇关于如何使用正则表达式查找和替换橡皮布改变整个应用程序中使用另一种语法记录的文章。请参阅this previous answerthis blog article

+0

+1感谢答案,我会看它太 – Zavael 2012-02-20 12:16:53

+0

是为我写的,我会看看它,看看哪些套房最适合我。如果我也可以检查这个答案是否正确,我会这么做:)也许在尝试后我会改变主意;) – Zavael 2012-02-20 14:22:35