C# .NET logging with NLog

After messing around with my own logging engine (boo!) I decided to play around with NLog, it looked much simpler to use and configure than log4net (not that log4net looked all that complicated, just that NLog seemed cleaner to me).

So grab the NLog setup package from SourceForge and install it. So that’s it. Yes, that simple really, the installer includes a series of templates that will be used later to create an empty configuration file.

I like using the Datestamp of YYYYMMDD_HHMM so 1:30 PM on April 1st, 1980 would be 19800401_1330. This is the configuration I used in the Nlog.config file to write the timestamp’ed log data to a text file (add a new file to your project and pick the ‘Empty NLog Configuration File” from the My templates section), this setup will also create a Datestamp on the log file:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <targets>
    <target name="file" xsi:type="File" fileName="${basedir}/${date:format=yyyyMMhh}.log.txt"
            layout="${date:format=yyyyMMhh_HHmm}|${level}|${stacktrace}|${message}"/>
  </targets>

    <rules> 
      <logger name="*" minlevel="Trace" writeTo="file"/>
    </rules>
</nlog>

Here is a example of using the logging:

using System;
using System.Collections.Generic;
using System.Text;
using NLog;

namespace NlogTester
{
    class Program
    {
        private static Logger _logger = LogManager.GetCurrentClassLogger();
        
        static void Main(string[] args)
        {
            _logger.Trace("This is a Trace message");
            _logger.Debug("This is a Debug message");
            _logger.Info("This is an Info message");
            _logger.Warn("This is a Warn message");
            _logger.Error("This is an Error message");
            _logger.Fatal("This is a Fatal error message"); 

            Console.ReadKey();

        }
    }
}
This creates an excellent log file that looks like this:
20080109_2136|Trace|AppDomain.ExecuteAssembly => AppDomain._nExecuteAssembly => Program.Main|This is a Trace message
20080109_2136|Debug|AppDomain.ExecuteAssembly => AppDomain._nExecuteAssembly => Program.Main|This is a Debug message
20080109_2136|Info|AppDomain.ExecuteAssembly => AppDomain._nExecuteAssembly => Program.Main|This is an Info message
20080109_2136|Warn|AppDomain.ExecuteAssembly => AppDomain._nExecuteAssembly => Program.Main|This is a Warn message
20080109_2136|Error|AppDomain.ExecuteAssembly => AppDomain._nExecuteAssembly => Program.Main|This is an Error message
20080109_2136|Fatal|AppDomain.ExecuteAssembly => AppDomain._nExecuteAssembly => Program.Main|This is a Fatal error message

Good stuff indeed.

Enjoy!

Ubuntu 7.10 (Gutsy) Server on VirtualBox

I wanted to setup an Ubuntu 7.10 (Gutsy) server to be a sandbox on my Vista notebook using VirtualBox. Seems there is a known problem with running the Ubuntu server kernel (defaults to PAE and the kernel reports 'CPU Too Old' on boot and bails, seem VirtualBox does not yet support PAE extensions) via VirtualBox, however this can be made to work by switching out to the generic kernel and using that. Sure it's not ideal for a production level server, but it works great in a sandbox mode.

Step-by-step 1, 2, 3 sort-of directions:

  • Download the Ubuntu 7.10 Server ISO (BitTorrent FTW!)
  • Download and install VirtualBox
  • ubuntu_server_virtualbox_setup Setup a new VM instance
    • Recommended settings: OS Type of Linux 2.6,  512MB RAM, 20GB Dynamic Drive, 8MB Video, USB + EHCI, Network NAT (see my post about port forwarding if you need to interact with this VM via your LAN machines).
  • Set the new VM instance CD/DVD-ROM to mount the ISO
  • Install with basically all defaults, let the CD eject and then the OS will reboot the VM
  • Boot again from your ISO mounted CD to get the initial install screen
  • b_ubuntu_server_virtual_boxThis time select the 'Rescue a broken system' option from the menu
  • Go back though like you are setting the system backup (keyboard, locale, hostname, etc.) this is just used to boot up the installer
  • c_ubuntu_server_virtual_box At the Rescue Mode screen be sure and choose your root (/) partition, in my case it was /dev/sda1
  • d_ubuntu_server_virtual_box At the '[!!] Enter rescue mode' screen pick the 'Execute a shell in /dev/sda1' option
  • From here you will be in a shell from your chrooted installed server, execute these commands (press 'Y' to install any needed additional packages  and press 'Y' to confirm):
    • apt-get update
    • apt-get install linux-image-generic
    • apt-get remove linux-server
  • ubuntu_server_editing_grub_menu Now you need to change Grub to use the linux-generic as the default item to boot from. This is done by setting the 'default x' value to be the number of the 'title' block in the menu.lst file. This should be the third block (two linux-server entries and then two more linux-generic entries):
    • nano -w /boot/grub/menu.lst
    • <find the 'default 0' and change to be the number of your linux-genric entry, should be 2 (remember computers count from 0, duh!)>
    • <with Nano its CTRL+X to save the file press 'Y' to confirm the overwrite>
    • exit
  • The 'exit' command should exit you out of the shell and then reboot the VM instance.
  • I would recommend doing both a Snapshot from within VirtualBox (this gives you a reference point in time position you can 'clean' your sandbox back to. Do this before you start monkeying around with your server, else you will have to start again to get it back in a pristine condition.
  • server_up_and_running Enjoy!

VirtualBox + Windows 2003 Server + LAN != working;

So I wanted to setup a Windows 2003 Development virtual machine instance using VirtualBox on my notebook and then have the ability to get to that server from the network. Well it seems that VirtualBox is not real intuitive about handling this setup. Seems that the VirtualBox Guest NAT only does that; NAT’s the outbound traffic from the VM via the Host network adapter and does not allow any inbound packets (not stateful NAT requests) to get back to the VM’s NIC.

So after much toiling around on the VirtualBox forums I found the answer, you can setup a port forward on the host machine to send the particular port traffic to the VM NIC. This is, for some odd reason I must say, is not done via the VirtualBox GUI but via the commandline tool ‘C:\Program Files\innotek VirtualBox\VBoxManage.exe’.

So let’s say you have a VM instance named ‘Win2003Dev’ and you want to send Subversion (port 3690) traffic to/from that VM you would perform this:

  • Close VirtualBox (this requires lock write to the VirtualBox.XML file), if running
  • Open up a command line console (if using Vista you should use  Start -> cmd -> CTRL+SHIFT+ENTER)
  • Enter these three lines (one at a time, doh!):
    VBoxManage setextradata "Win2003Dev" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/svn/Protocol" TCP
    VBoxManage setextradata "Win2003Dev" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/svn/GuestPort" 3690
    VBoxManage setextradata "Win2003Dev" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/svn/HostPort" 3690	
  • Note: The ‘svn’ I used in the lines is just something I picked, it seems you can use ‘hot-potato’ if you like. I thought something logical would make most sense.

Enjoy!

Blog entry via Windows Live Writer

So this post is coming straight from Windows Live Writer.

So far its an impressive piece of software, wonder how well this is going to work. I installed a couple of Plug-ins; Insert Polaroid, Insert Code, Paste from Visual Studio and Text Template.

Lets test some stuff out;

  • A Slashdot Link (should pop new window).
  • A small Icon Image (should be transparent PNG) -> agt_action_success
  • How about some HTML Code:
         1:  <html>
         2:  <body>
         3:  This is some HTML Code
         4:  </body>
         5:  </html>

       
  • And now a quote:
    • "Software is like sex: it’s better when it’s free" – Linux Torvalds

Very, very cool.

Favorite Wallpaper (via Lightbox)

I was wondering how this Lightbox extension for BlogEngine works.

Here is my current favorite wallpaper, (clicky pops 1280×1024).

A New Years Sunset  

 

<a rel="lightbox" href="path_to_image_to_pop" title="popup_title"><img src="path_to_thumbnail"></a>

It’s a blog eat blog world.

A blog? Why you ask?

Well its not meant to be a point of social discussion, or my witty rant's on my conspiracy theories. No, not so much. The intent of this feature filled experience is to basically document my experiences in technology, yes I use the word 'experiences' very loosely. My adventures, dis-adventures, wins, losses and throw-the-damn-thing-out-the-window experiences.

Bottomline, this is meant as a place where I can document things I experiment with hopefully to help me remember what I did to get it working in the first place.