|
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 Log4NetTest
{
public partial class frmMain : Form
{
//Here is the once-per-class call to initialize the log object
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
//You should try to call the logger as soon as possible in your application
log.Debug("Application started");
}
//This method hooks to the button on the strt of the form. When
//you click the button, you call every type of log method available
private void butRunTest_Click(object sender, EventArgs e)
{
log.Debug("Debug logging");
log.Info("Info logging");
log.Warn("Warn logging");
log.Error("Error logging");
log.Fatal("Fatal logging");
//This is where we call the logger from a different class
OtherClass.TestLogger();
try
{
throw new System.IO.FileNotFoundException();
}
catch (Exception ex)
{
log.Debug("Debug error logging", ex);
log.Info("Info error logging", ex);
log.Warn("Warn error logging", ex);
log.Error("Error error logging", ex);
log.Fatal("Fatal error logging", ex);
}
}
}
//Designed to show how we can change log levels by class or namespace
public static class OtherClass
{
//Here is the once-per-class call to initialize the log object
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
//Used to demonstrate making log calls from a different namespace
public static void TestLogger()
{
log4net.GlobalContext.Properties["testProperty"] = "This is my test property information";
log.Debug("Other Class - Debug logging");
log.Info("Other Class - Info logging");
log.Warn("Other Class - Warn logging");
log.Error("Other Class - Error logging");
log.Fatal("Other Class - Fatal logging");
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I am currently a Senior Software Developer at a company in Illinois called DeGarmo. My primary skills are in .NET, SQL, JavaScript, and other web technologies although I have worked with PowerShell, C, and Java as well.
In my previous positions, I have worked as a lead developer, professor and IT Director. As such, I have been able to develop software on a number of different types of systems and I have learned how to correctly oversee the overall direction of technology for an organization. I've developed applications for everything from machine automation to complete ERP systems.
I enjoy taking hard subjects and making them easy to understand for people unfamiliar with the topic.