Solved

Log4Net custom Appender not being fired.

Posted on 2016-10-14
5
140 Views
Last Modified: 2016-11-26
I have created a simple log4net custom appender, code below, to access a JSON Web Service.  Everything seems to initialize correctly, but the Append method is never called.  Can someone please tell me what I am doing wrong.  Thanks

public class RMAppender : AppenderSkeleton
    {
      
        protected override void Append(LoggingEvent loggingEvent)
        {
            var url = ConfigurationManager.AppSettings["EventLogServer"];
            if (string.IsNullOrEmpty(url)) return;

            var client = new JsonServiceClient(url);

            var data = loggingEvent.MessageObject as IList<string>;

            if (data != null)
            {
                foreach (var s in data)
                {
                    // Do something with each string
                }
            }

            var log = new LogEntry
            {
                CurrentAppDomain = loggingEvent.Domain,
                Logger = loggingEvent.LoggerName,
                Date = DateTime.Now
            };

            var response = client.Post<LogEntry>(log);
       }
    }

Open in new window


This is configured in code using:

private static void ConfigureLog()
        {
            var root = ((Hierarchy)LogManager.GetRepository()).Root;

#if (DEBUG)
            root.AddAppender(GetConsoleAppender());
#endif

            var append = new RMAppender();
            append.Threshold = Level.All;
            append.ActivateOptions();

            root.AddAppender(append); 
            
            root.Repository.Configured = true;
        }

Open in new window


I have used this same method for adding other appenders including EventLog, SMTP etc.  and they all work.  It is only the custom one that Append is never called.

I checked root, and following the above, the appender is listed under root.Appenders, and appears to be configured the same way as all the other appenders.

This has got me really stumped.  I am sure I am missing something, but I cannot see what.
0
Comment
Question by:townsma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41883679
could you try tho config like this (you've to change yournamespace) :

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

Open in new window


check this page : http://www.alteridem.net/2008/01/10/writing-an-appender-for-log4net/
0
 
LVL 6

Author Comment

by:townsma
ID: 41884427
So you are saying is that we can only use a custom appender if we use an external config file?  Because all out log4net config is hard-coded into the application, as per my sample above.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 41884593
it's just to be sure it work fine like that...
0
 
LVL 6

Author Comment

by:townsma
ID: 41902736
My apologies for the delay in getting back to you. I did look at the advice above, and tried to incorporate it, however, because the remainder of the class uses hard coded settings, it was causing confusion between the config file and the hard coded settings.  

At the same time someone one suggestion I take a look at a different, paid for, product. Kellerman .Net Logging.   This seemed to have all we required, and at a reasonable price.  It was very easy to plug in, as many of the calls are almost identical, and it worked first time with my Custom Appender.  So we have decided to abandon log4net, and stick with .Net Logging.

So many thanks for your help.  If I have time in the future I will make a new class from scratch that just uses config files to test it out, if it works I will post back here for others.
1

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question