Monday, May 21, 2012
 
The best way to analyze your logs! Minimize
 Log4Net Mail archive   

The Log4Net mailing list is a great source of information about using log4Net, in this forum we collect all the messages in the log4net user list and some selected threads from the developer list.

Subject: Trying to set up an EXTREMELY SIMPLE event log feature... and failing terribly! Please help a newbie
Prev Next
You are not authorized to post a reply.

Author Messages
talktopete

11/11/2008 10:34 PM  


Hi and thanks in advance for the help.
I'm tearing my hair out over here trying to get this to work: a siple logger
that writes to the application section of the event log.

This is using log4net 1.2.10.0
Here is the app config:
--------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net>
    <appender name="EventLogAppender"
type="log4net.Appender.EventLogAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="EventLogAppender" />
    </root>
  </log4net>
</configuration>
--------------------------------------------------------------------
here is my code:
  log4net.Config.XmlConfigurator.Configure();
  logger = LogManager.GetLogger("EventLogAppender");
  logger.Error("test");
-------------------------------------------------------------------

No errors, nothing happens! I notice that there are no appenders.
nothing is written to the event log.

I am tearing my hair out trying to figure out what is going on, can somebody
help?
--
View this message in context: http://www.nabble.com/Trying-to-set-up-an--EXTREMELY-SIMPLE-event-log-feature...-and-failing-terribly%21-Please-help-a-newbie-tp20448428p20448428.html
Sent from the Log4net - Users mailing list archive at Nabble.com.

Francine Taylor

11/11/2008 11:05 PM  

You didn't mention what type of application this is being run from.  I
have two suggestions for error checking, don't know if they will help.

The first is that if you are getting errors, you won't be able to see
them unless you are running from a console application (or unless you
set up a trace appender to grab the errors, I haven't done that so
you'll have to google for it).  This will tell you if the problem is
with the error logging itself.

The second is a method which will tell you how your log4net is
configured.  It's written in C#, so you may need to translate.  This
should tell you if it is your log4net configuration process which is
going astray.

        public static string GetHierarchyInformation() {

            Hierarchy hier = (Hierarchy)LogManager.GetRepository();

            StringBuilder sb = new StringBuilder();

            // first, look for it in the root
            sb.AppendLine("Root Information:");
            sb.Append("   Name: " + hier.Name);
            sb.AppendLine("   Threshold: " + hier.Threshold.ToString());

            sb.Append("   Root Appenders: ");
            foreach (IAppender a in hier.Root.Appenders) {
                sb.Append(a.Name + ", ");
            }
            sb.AppendLine("");
            sb.AppendLine("Hierarchy Appenders:");
            foreach (IAppender a in hier.GetAppenders()) {
                sb.AppendLine(FormatAppenderLine(a));
            }

            sb.AppendLine("Loggers:");
            foreach (ILogger l in hier.GetCurrentLoggers()) {

                Logger log = (Logger)l;
                sb.Append("   " + log.Name);
                sb.Append("  Additivity: " + log.Additivity.ToString());

                sb.Append("  Level: ");
                if (log.Level == null) {
                    sb.Append("null");
                }
                else {
                    sb.Append(log.Level.ToString());
                }
                sb.AppendLine("");
                foreach (IAppender appender in log.Appenders) {
                    sb.AppendLine(FormatAppenderLine(appender));
                }
            }
            return sb.ToString();
        }
        private static string FormatAppenderLine(IAppender appender) {
            StringBuilder sb2 = new StringBuilder();
            sb2.Append("      Appender: " + appender.Name);
            sb2.Append("  Type: " + appender.GetType().ToString());

            if (appender is log4net.Appender.FileAppender) {
                FileAppender app = (FileAppender)appender;
                sb2.Append("  File: " + app.File);
            }
            else if (appender is log4net.Appender.RollingFileAppender) {
                RollingFileAppender app = (RollingFileAppender)appender;
                sb2.Append("  File: " + app.File);
            }
            else if (appender is log4net.Appender.SmtpAppender) {
                SmtpAppender app = (SmtpAppender)appender;
                sb2.Append("  To: " + app.To);
            }
            return sb2.ToString();
        }

talktopete

11/11/2008 11:25 PM  


This IS a console application, .net 3.5, and the app.config file is set to
always copy on build, so that rules out number one.

I ran the code you suggested, and yielded this information:
---------------------------------------------------------------------------
Root Information:
   Name: log4net-default-repository   Threshold: ALL
   Root Appenders: EventLogAppender,
Hierarchy Appenders:
      Appender: EventLogAppender  Type: log4net.Appender.EventLogAppender
Loggers:
   EventLogAppender  Additivity: True  Level: null
--------------------------------------------------------------------------

Does anything in there jump out at you, such as the "Level" perhaps?

gary.grubbs@redriversystems.com

11/11/2008 11:50 PM  

Try using "System.Configuration.IgnoreSectionHandler" as the type for the configuration section handler.

talktopete

11/12/2008 12:42 AM  


I set my config section handler to:
<configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"
/>
  </configSections>

and it gave the exact same behavior, no error messages, and nothing in the
event log.:confused:

gary.grubbs@redriversystems.com

11/12/2008 12:59 AM  

I tried your code, as originally posted, locally and it works with the addition of the declaration of the logger variable ("ILog logger;").  The message should output to the "Application" log.  Couple of questions:
Are you expecting it to create and log to a "EventLogAppender" log?
Have you tried running this locally and checking your local event log?
If no, have you validated that security is correctly set/configured to allow you to write to the event log of the remote machine?

Ron Grabowski

11/12/2008 1:02 AM  

The first thing I would is to make sure log4net itself is working. Your config file runs correctly when I add a ConsoleAppender:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
      <layout type="log4net.Layout.SimpleLayout" />
    </appender>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.SimpleLayout" />
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="EventLogAppender" />
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>
</configuration>

I can also see the entries in the Event Log. Internal debugging:

  <appSettings>
    <add key="log4net.Internal.Debug" value="true" />
  </appSettings>

didn't show any problems. Do you get an exceptions when internal debugging is turned on? From what I can tell it looks like there's something wrong with the machine its running on.

talktopete

11/12/2008 1:18 AM  

By golly, you got it!!!

By changing my layout to log4net.Layout.SimpleLayout, it started working!
The problem was with the layout format:
------------------------------------------------------------------------------
"%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"
------------------------------------------------------------------------------
something in there caused the logger to fail. What is annoying is how
silently it failed, I wish there was an option to make log4net throw
verbose, easy-to-understand exceptions!

I got the layout string from various examples on the google. Go figure
someone posted code that fails...

You are not authorized to post a reply.
Forums > Log4Net > Log4Net Mail archive > Trying to set up an EXTREMELY SIMPLE event log feature... and failing terribly! Please help a newbie



ActiveForums 3.7

 

 

 

 

 

 

 

 

Log4Net Dashboard

Log analysis and monitoring made easy!

Log4Net Dashboard is a log viewer that can read log statements from a variety of logging output targets.

You can download a free developer version.

  

Check it out!

On the demonstration site you can try it  with live data.demo.l4ndash.com - Try Log4Net Dashboard with live data

The mail archive is a copy of all the mail sent to the mail address: log4net-user@logging.apache.org, organized as a forum.

If you would like to participate in the mail list, send a mail to log4net-user-subscribe@logging.apache.org.

More information about the mailing list is available on: http://logging.apache.org/log4net/support.html

 

A complete topic list is available and can be viewed here (warning, it takes some time to load)

 

Copyright 2005-2008 by FaktNet AS Terms Of Use Privacy Statement