Log4Net custom appender : How to log messages using a custom appender?
Asked Answered
B

1

11

Issue was solved - I edit this post with the right code.

I am trying to wrote the "main" function that initialize the log4net logger + attachment to the Custom appender and send message thought it - this is my try (without success Unfortunately)

What is wrong with my initialize (Form1.cs below)?

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
         ILog log = LogManager.GetLogger(typeof(Form1));

        public Form1()
        {

            log4net.Config.XmlConfigurator.Configure();
            InitializeComponent();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            log.Info("Creating log");
        }
    }

Error message -Exception = {"Could not load file or assembly 'MessageBoxAppender' or one of its dependencies. The system cannot find the file specified.":"MessageBoxAppender"} [IMG]http://i57.tinypic.com/qrjcjc.png[/IMG]

I try to write logging messages with this custom appender code from the link below

http://www.alteridem.net/2008/01/10/writing-an-appender-for-log4net/

My goal is to click on a button and a log message will write thought the custom appender.

I have 3 files/class.

1.Form1.cs windows form – contain only a button that should write a message and the initialize.

2."MessageBoxAppender.cs" - the custom appended that inherit from "AppenderSkeleton"

3.app.config - for the log4net configuration

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net"
    type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="MessageBoxAppender"
        type="WindowsFormsApplication1.MessageBoxAppender, WindowsFormsApplication1">
      <layout type="log4net.Layout.PatternLayout">
        <ConversionPattern value="%m" />
      </layout>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="MessageBoxAppender" />
    </root>
  </log4net>
</configuration>

MessageBoxAppender custom appender

    using log4net.Appender;
    using log4net.Core;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        public class MessageBoxAppender : AppenderSkeleton
        {
            /// <summary>
            /// Writes the logging event to a MessageBox
            /// </summary>
            override protected void Append(LoggingEvent loggingEvent)
            {
                string title = string.Format("{0} {1}",
                    loggingEvent.Level.DisplayName,
                    loggingEvent.LoggerName);

                string message = string.Format(
                    "{0}{1}{1}{2}{1}{1}(Yes to continue, No to debug)",
                    RenderLoggingEvent(loggingEvent),
                    Environment.NewLine,
                    loggingEvent.LocationInformation.FullInfo);

                DialogResult result = MessageBox.Show(message, title,        MessageBoxButtons.YesNo);

                if (result == DialogResult.No)
                {
                    Debugger.Break();
                }
            }

            /// <summary>
            /// This appender requires a <see cref="Layout"/> to be set.
            /// </summary>
            override protected bool RequiresLayout
            {
                get { return true; }
            }
        }
    }
  1. I am not sure that this line in the app.config is correct - was answered

    <appender name="MessageBoxAppender"
    type="WindowsFormsApplication1.MessageBoxAppender, MessageBoxAppender">
    </appender>
    

Is the convention is

 type="namespace + custom appender class name, custom appender class name>  

[Edit] i add to my code:

var errors = LogManager.GetRepository().ConfigurationMessages.Cast<log4net.Util.LogLog>();
Bedder answered 6/11, 2014 at 17:20 Comment(2)
log4net is designed not to throw an error if the configuration is invalid, but you can query for configuration errors by calling var errors = LogManager.GetRepository().ConfigurationMessages.Cast<LogLog>(); after the call to XmlConfigurator.Configure();Hare
Thanks, i add it. get this error "Exception = {"Could not load file or assembly 'MessageBoxAppender' or one of its dependencies. The system cannot find the file specified.":"MessageBoxAppender"}"Bedder
J
18

The value to use for the type attribute is a fully-qualified name of the class. This is the full path to the class for the appender (namespace + class name), followed by the name of the assembly that it is in. For your code, this will be (assuming that your assembly is called WindowsFormsApplication1 - you can check this in the properties of your project):

<appender name="MessageBoxAppender"
    type="WindowsFormsApplication1.MessageBoxAppender, WindowsFormsApplication1">
</appender>
Jimjams answered 6/11, 2014 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.