McInnes Family Blog

June 25, 2009

log4net example

Filed under: .NET Programming — admin @ 2:46 pm

 

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

June 16, 2009

MVC Training Session by Eric Hexter

Filed under: Uncategorized — admin @ 11:16 am

Model View Controller (MVC) pattern, been around for a long time, now implemented in ASP.NET MVC v1.0

Controller – Responsible for WHAT a screen does
Views – Responsible for DISPLAYING the screen
Models – Responsible for REPRESENTING the task

Advantages: decouples rendering logic from handling user input, decouples screen logic from processing the request, leverages interfaces to separate responsibilities, provides complete control over markup and urls

Look at mvccontrib.testhelper class

It has an AssertViewRendered method which can be used for testing

June 13, 2009

Another installation is in progress, when trying to install iTunes on Windows Vista

Filed under: Uncategorized — admin @ 1:37 pm

 

Set the "Windows Installer" service to disabled, restart system. Upon restart, set the service to manual and try reinstalling application.

June 10, 2009

Upgrade firmware on iPhone

Filed under: Technology and Software — admin @ 6:58 am

 

Hold down the SHIFT key (Windows) on your keyboard while clicking on the Check for Update button (not the restore button!). A file browsing dialog should appear, and you must select the firmware file you downloaded in the previous step.

June 9, 2009

How to Open an MVC project in Visual Studio 2010

Filed under: .NET Programming — admin @ 11:23 am

 

http://weblogs.asp.net/leftslipper/archive/2009/01/20/opening-an-asp-net-mvc-project-without-having-asp-net-mvc-installed-the-project-type-is-not-supported-by-this-installation.aspx

May 28, 2009

Fix audio with Audacity

Filed under: Technology and Software — admin @ 7:48 pm

 

  1. Import file from Project Menu
  2. Apply Equalization filter, RCA Victor 1938
  3. Apply Gain
  4. Select part of track with just noise, choose Noise Removal
  5. Select all
  6. Choose Noise Removal, Less, Preview and Apply filter

Example IsapiRewrite4.ini File for IsapiRewrite4

Filed under: Technology and Software — admin @ 3:19 pm

 

– 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]

Duplicate VM for Hyper-V

Filed under: Technology and Software — admin @ 2:48 pm

 

  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

May 27, 2009

What is Worship?

Filed under: Wednesday Morning — admin @ 4:46 am


Psalm 100:4 (New International Version)

4 Enter his gates with thanksgiving and his courts with praise; give thanks to him and praise his name.

 

Rules without Relationship = Rebellion

 

"Similarly, encourage the young men to be self-controlled. In everything set them an example by doing what is good". Titus 2:6-7.

http://www.coachtonypierceoutreach.org/info/dads2.htm

May 22, 2009

Simple StructureMap (DI) example

Filed under: .NET Programming — admin @ 10:20 am

 

using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using StructureMap;

namespace VI.Interfaces
{
    [TestFixture]
    public class TestInjection
    {

        [SetUp]
        public void SetupTests()
        {
            // Initialize the static ObjectFactory container
            ObjectFactory.Initialize(x => x.ForRequestedType<IException>().TheDefaultIsConcreteType<VIException>());
        }

        [Test]
        public void TestConcreteClassAccess()
        {
            IException ConcreteInstance = ObjectFactory.GetInstance<IException>();
            Exception ex = new Exception("My exception message", new Exception("My inner exception message"));
            ConcreteInstance.GetException(ex);
            System.Diagnostics.Trace.WriteLine("Concrete Class exception: " + ConcreteInstance.GetException(ex));
        }

    }

    interface IException
    {
        string GetException(Exception ex);
        List<string> GetExceptions(List<Exception> exList);
    }

    public class VIException : IException
    {

        string IException.GetException(Exception ex)
        {
            return ex.Message;
        }

        List<string> IException.GetExceptions(List<Exception> exList)
        {
            return new List<string> { "item 1", "item 2" };
        }

        #endregion
    }
}

Older Posts »

Powered by WordPress