Foo

without comments

foo bar

Written by grantmcinnes

August 7th, 2009 at 9:55 am

Posted in Uncategorized

log4net example

without comments

 

In a recent project, I needed to implement logging and decided to use the log4net.dll which I have seen referenced in a number of different open source projects. Here are the steps I had to taken in order to use log4net in my project:

 

1. Download the log4net.dll from http://sourceforge.net/projects/log4net/

2. Add a reference to your project that includes the downloaded log4net.dll

3. Add a log4net section to your web.config/app.config file:

<configuration>
    <configSections>
            <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
        </sectionGroup>
    </configSections>
</configuration>

4. Add a log4net.config file to your project with the following configuration:

<?xml version="1.0" encoding="utf-8"?>
<log4net debug="true">
  <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
    <param name="File" value="log4netoutput.log" />
    <param name="AppendToFile" value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p [%c] %m%n" />
    </layout>
  </appender>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="LogFileAppender" />
  </root>
</log4net>

There are various Appenders you can create but this example uses a text file to log the results into the bin directory of the application. There are also different values that can be used as part of the ConversionPattern to get different output.

5. Be sure to change the properties of the log4net.config file in your project for the Copy to Output Directory attribute to Copy Always. I spent hours debugging my code because the config file was not being copied into the bin directory and log4net did not throw any exceptions. Since I had not used log4net before, I was making the assumption the problem was with my code and/or config file settings.

6. Here’s my code from a test method to make sure log4net is working correctly:

[Test]
public void TestLog4NetOnItsOwn()
{
    ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    XmlConfigurator.Configure(new FileInfo(Settings.Default.log4net_config_file)); 
    logger.Debug("Here is a debug log.");
}

To log results to the console, include the following line of code:

BasicConfigurator.Configure();

 

I’ve seen other posts that reference the log4net.config file in the Assembly.cs file rather than in code:

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

 

Other Posts and Resources:

http://it.toolbox.com/blogs/daniel-at-work/using-log4net-in-cnet-26794

http://geekswithblogs.net/bsherwin/archive/2008/02/15/119657.aspx

Written by grantmcinnes

June 25th, 2009 at 2:45 pm

Posted in .NET Programming

How to Open an MVC project in Visual Studio 2010

without comments

Written by grantmcinnes

June 9th, 2009 at 11:25 am

Posted in .NET Programming

Example IsapiRewrite4.ini File for IsapiRewrite4

without comments

 

– Redirect attacks

RewriteCond %{REMOTE_ADDR} 10.10.1.0
RewriteRule ^/(.*)$ /$1 [F]

– Redirect from http://foo.com to http://www.foo.com

RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]

Written by grantmcinnes

May 28th, 2009 at 3:20 pm

Posted in Technology

Duplicate VM for Hyper-V

without comments

 

  1. Shutdown the VM that you want to duplicate
  2. Make a copy of the VHD file
  3. Create a new VM that joins to the copied VHD file
  4. Start the VM
  5. Launch SysPrep (c:\windows\system32\sysprep.exe) over the new VM once started
  6. Relicense Windows, rename Machine Name

Written by grantmcinnes

May 28th, 2009 at 2:27 pm

Posted in Technology

Regular Expressions Cheat Sheet (V2)

without comments

Written by grantmcinnes

May 20th, 2009 at 3:55 pm

Posted in .NET Programming

Eric Hexter’s Current .NET Application and Tooling Stack

without comments

Written by grantmcinnes

May 20th, 2009 at 3:01 pm

Posted in .NET Programming

Liked “The Conversation Prism”

without comments

 

 

Written by grantmcinnes

May 1st, 2009 at 1:39 pm

Posted in Web 2.0

Roadmap to web 2.0 in healthcare organizations: Slideshow

without comments

Written by grantmcinnes

May 1st, 2009 at 8:32 am

Posted in Medicine,Technology

Twitter Use Examples

without comments

 

Commercial use of Twitter: Advertising job vacancies:

image

Written by grantmcinnes

April 30th, 2009 at 1:20 pm

Posted in Uncategorized