9 Replies - 8227 Views - Last Post: 22 November 2010 - 09:08 AM Rate Topic: -----

#1 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

C# log4net configuration

Posted 17 November 2010 - 02:34 PM

Well there must be something terribly simple messed up here so maybe someone can point me in the right direction.

Ok So I added log4net.dll to the project references.
I added a log4net.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="ConsoleAppender" />
    </root>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="[Header]\r\n" />
        <param name="Footer" value="[Footer]\r\n" />
        <param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
      </layout>
    </appender>
  </log4net>
</configuration>


I have added two lines to my AssemblyInfo.cs

using log4net.Config;

[assembly: XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]


and here is my program:
using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using log4net.Config;

namespace MyProgram
{
    class Program
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(Program));

        static Program()
        {
           
        }

        static void Main(string[] args)
        {
            logger.Debug("Once upon a midnight dreary");
            System.Console.ReadKey();
        }
    }
}


No messages logged... I can't see that I skipped over anything.

If I try to add the configuration information to the app.config the program will not compile and so following the advice from here and the tutorial here I tried adding the lines to the assemblyinfo.

This is a really stupid point that I don't have time to get bogged down on. Anyone see what dumb*&^% mistake I am making?

Is This A Good Question/Topic? 0
  • +

Replies To: C# log4net configuration

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: C# log4net configuration

Posted 17 November 2010 - 02:47 PM

I think you may need to add a <logger> element in your config.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <logger name="Program">
      <level value="DEBUG" />
      <appender-ref ref="ConsoleAppender" />
    </logger>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="ConsoleAppender" />
    </root>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="[Header]\r\n" />
        <param name="Footer" value="[Footer]\r\n" />
        <param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
      </layout>
    </appender>
  </log4net>
</configuration>



That's how I usually do it, and it looks OK to me otherwise.

EDIT: Although you'd EXPECT that it would pick up the root logger and run with it. Hmmm.

This post has been edited by JackOfAllTrades: 17 November 2010 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: C# log4net configuration

Posted 17 November 2010 - 02:55 PM

I found the solution. Properties > "Copy to output directory":"Always" -- and that did it.

As for the logger -- I had it set for the root logger which is fine, in the end I will probably establish a few loggers as I need to do several different kinds of logging (including an email notification). I could not even get my test app to work so I was freakin' out.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: C# log4net configuration

Posted 17 November 2010 - 02:58 PM

Cool! Yeah, I have a bunch of loggers for the different modules in my app. Once you get log4net up and running, it rocks. Pretty fine control and flexibility.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: C# log4net configuration

Posted 17 November 2010 - 03:08 PM

yes as a Java developer I am a big fan of log4j and so I figured that if even half of the features are available in log4net I would have a solid foundation for the logging.

Specifically what I am hoping is supported is NDC's (which seem to "exist for compatibility with older versions of log4net") -- I need to track a particular operation as it moves though its lifecycle even though there are several "disconnects" where the application will go off and do other things.


So for example my application will receive a message say ID555 and then process it and submit that message to another system. The other system will eventually return a response (maybe today, maybe next week but definitely not immediately). I want to be able to keep a continuous log of the activities of ID555 as it is processed. Log4J could do it, and I *think* log4net can as well.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: C# log4net configuration

Posted 17 November 2010 - 03:20 PM

Interesting...let us know if it works. Might come in handy for me in the future. I use thread contexts in a using() block strictly to keep track of the current command being executed, I don't do anything too fancy.
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: C# log4net configuration

Posted 17 November 2010 - 03:28 PM

In web apps I often use context to keep track of a particular user session or transaction. that way I can know exactly which little &^$#-head found it funny to register with the name: "BOB;DROP TABLE USERS" :)
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: C# log4net configuration

Posted 17 November 2010 - 03:50 PM

Damn that Bobby Tables! :)
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4311
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: C# log4net configuration

Posted 17 November 2010 - 04:03 PM

Posted Image

This is the second XKCD that I've linked today.
Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: C# log4net configuration

Posted 22 November 2010 - 09:08 AM

Does log4net work with Framework 3.5?

So I added log4net to my main project but I am getting the error:

The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?)


on the line:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = ".config", Watch = true)]


The line (and file log4net.config) are copied directly over from the working project so I am a bit confused as to why it is not working on the main project.

When I double click on log4net in the references Rather than going to log4net namespace it goes to Accessibility [2.0.0] -- which is what makes me think maybe it is an issue with versions?


Crisis averted! -- Closed the other project using the dll and it works just fine. WHEW!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1