Configuring log4net TextBoxAppender (custom appender) via Xml file

后端 未结 7 2084

This is in followup to my question: Flexible Logging Interface...

I now want to write a custom log4net appender for a multiline TextBox, for my WinForms 2.0 applicat

相关标签:
7条回答
  • 2020-12-03 05:52

    Above sample by Klodoma is quite good. If you change the textbox to a richtextbox, you can do more with the output. Here is some code to color code messages by level:

            System.Drawing.Color text_color;
    
            switch (loggingEvent.Level.DisplayName.ToUpper())
            {
                case "FATAL":
                    text_color = System.Drawing.Color.DarkRed;
                    break;
    
                case "ERROR":
                    text_color = System.Drawing.Color.Red;
                    break;
    
                case "WARN":
                    text_color = System.Drawing.Color.DarkOrange;
                    break;
    
                case "INFO":
                    text_color = System.Drawing.Color.Teal;
                    break;
    
                case "DEBUG":
                    text_color = System.Drawing.Color.Green;
                    break;
    
                default:
                    text_color = System.Drawing.Color.Black;
                    break;
            }
    
            _TextBox.BeginInvoke((MethodInvoker)delegate
            {
                _TextBox.SelectionColor = text_color;
                _TextBox.AppendText(RenderLoggingEvent(loggingEvent));
            });
    

    If you really want to, the colors could be mapped from the log4net config in the same fashion as the ColorConsoleAppender, but I leave that for the next coder to stumble onto this sample...

    0 讨论(0)
提交回复
热议问题