Hello, read this:
http://mail-archives.apache.org/mod_mbox/logging-log4net-user/200805.mbox/%[email protected]%3e
It should help you
Radovan 

-----Původní zpráva-----
Od: mhgms2 [mailto:[email protected]] 
Odesláno: 3. dubna 2009 17:18
Komu: [email protected]
Předmět: Newbie help for programatically creating log without a config file

Hi:

I am trying to create a test program that configures a logger completely from 
code (I want to avoid a config/xml file for this exercise), but although I'm 
not getting any errors, no log file is created.

Can anyone help?

Regards,
Martin.

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace log4net
{
  public partial class FrmMainFormLog4Net : Form
  {
    private enum LogDestination { None, File, Access, MsSql }

    private static LogDestination logDestinationType = LogDestination.None;
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
      System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public FrmMainFormLog4Net()
    {
      InitializeComponent();
      //log4net.Config.XmlConfigurator.Configure();  //--- Load from App.Config 
file
      //log4net.Config.BasicConfigurator.Configure(); //--- Use Console output
      setupLogDestino(LogDestination.File);
    }

    private void button_Click(object sender, EventArgs e)
    {
      if(sender == btnDebug)
        log.Debug("*Debug message");
      else if(sender == btnInfo)
        log.Info("**Information message");
      else if(sender == btnWarn)
        log.Warn("***Warning message");
      else if(sender == btnError)
        log.Error("****Error message");
      else if(sender == btnFatal)
        log.Fatal("*****Fatal message", new Exception("My exception message"));
    }

    private void destino_CheckedChanged(object sender, EventArgs e)
    {
      RadioButton rb = sender as RadioButton;
      if(rb != null)
      {
        if(rb == rbFile && rb.Checked)
          setupLogDestino(LogDestination.File);
        else if(rb == rbAccess && rb.Checked)
          setupLogDestino(LogDestination.Access);
        else if(rb == rbMsSql && rb.Checked)
          setupLogDestino(LogDestination.MsSql);
      }
    }

    private static void setupLogDestino(LogDestination destination)
    {
      if(destination != logDestinationType)
      {
        log4net.Layout.PatternLayout layout = new 
log4net.Layout.PatternLayout("%d [%t] %-5p %c [%x] <%X{auth}> - %m%n");
        switch(destination)
        {
          case LogDestination.File:
            {
              string logFileName = System.IO.Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory,
                
System.IO.Path.ChangeExtension(AppDomain.CurrentDomain.FriendlyName,
".log"));
              log4net.Appender.FileAppender appender = new 
log4net.Appender.FileAppender
                {
                  AppendToFile = true,
                  File = logFileName,
                  Layout = layout,
                  Threshold = log4net.Core.Level.All
                };
              log4net.Config.BasicConfigurator.Configure(appender);
            }
            break;
          case LogDestination.Access:
            //--To do
            break;
          case LogDestination.MsSql:
            //--To do
            break;
        }
      }
    }
  }
}

Reply via email to