Ok, so given a WinForms app, I have a 'myapp.log4net' file (
https://gist.github.com/2070790) which gets copied to the output directory.
This file contains an AdoNetAppender and an SmtpAppender. The database
table is included in the l4n documentation I believe. Note the <root> tag
at the end of the file, that would let you add and remove appenders easily
in the XML, but just don't forget it. Then when initialising the app (in
Main()) I have the following:

    var log = LogManager.GetLogger(typeof(MainForm));
    XmlConfigurator.Configure(new FileInfo("myapp.log4net"));

`log` is then added to the IoC container and used through the app. I used
the same method for a couple of Windows services and ASP.NET (including
MVC) sites (in Global or MvcApplication's Application_Start() method),
although I used the following initialisation code one site:

            var log = LogManager.GetLogger("MyWeb");    // MyWeb is the
namespace, typeof(Global)
            XmlConfigurator.Configure(new
FileInfo(Server.MapPath("~/myweb.log4net")));

I don't know if that variation was required, or was just the first thing I
tried that worked.

WCF services hosted in IIS seem to be different, the discrete `xx.log4net`
configuration file didn't work due to something to do with the service not
being able to find the file. The XML contained in the .log4net file (ie
<log4net>..</log4net>) has to be copied verbatim into the web.config file
within the main <configuration> tag, and the log4net section declared in
the configSections tag:

  <configSections>
    <section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

Then the initialisation code has to go in a ServiceHostFactory
implementation's CreateServiceHost() method for the WCF service - IIS
hosted WCF services don't have a clear lifecycle like ASP.NET afaik. The
init code is:

            XmlConfigurator.Configure();
            var log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// not sure why typeof(MyHostFactory) wouldn't have worked.

This method can also be used with ASP.NET to avoid having a seperate
.log4net file.

Ben



On Sun, Mar 18, 2012 at 6:34 PM, Greg Keogh <[email protected]> wrote:

> Does anyone here use log4net? I used it several years ago but found it to
> be huge overkill because all I ever wanted to do was write everything to
> rolling files. I eventually wrote my own lightweight ‘RollingFile’ class
> and have used it for years. Now my logging needs have become a bit more
> complicated and I see that the latest log4net has expanded greatly to have
> all sorts of fancy appenders and filters. I quite like the idea that I can
> send severe error messages via email and boring stuff to a database (or
> lots of other interesting combinations like UDP and the event log).****
>
> ** **
>
> I’ve spent some spare time over the last few days trying to get the
> simplest example of writing to the TraceAppender or ConsoleAppender, but no
> matter what I do with config files and code I get no output. I have pasted
> lines out of dozens of examples and web searched until my eyes bleed
> without hope. Stepping deep inside tells me that the library “is not
> configured” and it does nothing.****
>
> ** **
>
> Could any kind person give me a minimal example of some code and a config
> file that writes “Hello” to a TraceAppender so it appears in the output
> window of VS2010? I am utterly stumped and at wits end.****
>
> ** **
>
> Thanks, Greg****
>

Reply via email to