Ads by Lake Quincy Media
Gibraltar - Learn about the best analysis tool for NLog

NLog 2.0 for Silverlight 4 and .NET Framework 4.0 preview builds

This week .NET Framework 4.0 and Silverlight 4 have been released. I’ve updated NLog 2.0 to support them and published a new build – very experimental – on CodePlex. One of the biggest updates in Silverlight 4 is support for out-of-browser applications with elevated permissions, which means applications that can access the filesystem.

I’ve put together a tiny sample that shows how to use NLog in Silverlight application. Basically since Silverlight does not have a concept of application configuration file you should configure Silverlight at application startup. In my case I’ve added this code in App.xaml.cs:

private void Application_Startup(object sender, StartupEventArgs e)
{
    InitializeNLog();
    this.RootVisual = new MainPage();
}

private void InitializeNLog()
{
    SimpleConfigurator.ConfigureForTargetLogging(
        new FileTarget()
        {
            FileName = "${specialfolder:MyDocuments}/log.${shortdate}.txt",
            Layout = new CsvLayout()
            {
                Columns =
                {
                    new CsvColumn("Time", "${longdate}"),
                    new CsvColumn("Level", "${level}"),
                    new CsvColumn("Lessage", "${message}"),
                    new CsvColumn("Logger", "${logger}"),
                },
            }
        },
        LogLevel.Debug);
}

The application will produce CSV-formatted log file in Documents folder. The name of the file will be log.CURRENTDATE.txt.

Usage of NLog stays unchanged:

public partial class MainPage : UserControl
{
    private static Logger logger = LogManager.GetCurrentClassLogger();

    public MainPage()
    {
        InitializeComponent();

        // log some events
        this.Loaded += (sender, e) => logger.Info("Page loaded");
        this.LayoutUpdated += (sender, e) => logger.Debug("Layout updated");
        this.SizeChanged += (sender, e) => logger.Debug("Size changed to {0}x{1}", e.NewSize.Width, e.NewSize.Height);
        this.KeyDown += (sender, e) => logger.Debug("Key down '{0}'", e.Key);
        this.Unloaded += (sender, e) => logger.Info("Unloaded");
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        logger.Info("Button clicked");
    }
}

Why not give NLog for Silverlight a try? Go to http://nlog.codeplex.com/releases/view/43702 and download preview bits today and report back any issues.

Ads by Lake Quincy Media

7 Responses to “NLog 2.0 for Silverlight 4 and .NET Framework 4.0 preview builds”

  1. Erik says:

    Out of curiosity, why do you consider these builds so experimental? If they pass all your unit tests, they should be betas rather than “experimentals” ;-)

  2. Jarek Kowalski says:

    Sure, they all pass the unit tests, but given the scope of changes I don’t feel confident calling them betas just yet. I still have a bunch of big changes to make (the biggest one is asynchronous wrappers to support Silverlight). Only then I will be able to call this thing Beta.

  3. dudih says:

    It’s not working, im running the sample code on vs2010 in win 7, change the folder to FileName = “d:/log/log.${shortdate}.txt” set the rights for the log folder to be writable but the log file is never there.

  4. Asgeir says:

    I get the same error as dudih. using vs2010 in win7, the first time i run the code, it worked, but after i deleted the log file and tried to compile the code and try again, it failed….

  5. Scott says:

    Hi Jarek,

    How does nlog deal with silverlight being asynchronous?

    For instance you fetch data from a ria service, but that’s asynchronous. So the order logs come back would seem to be a problem.

    Does the new silverlight version you posted address any such concerns or do you have advice on how to handle asynchronous issues with nlog?

    Thanks!

  6. buba4ko says:

    Hello, I could not make this sample work using VS 2010 on windows XP.

    Here are my steps:
    1. I installed the latest NLog2.netfx40-Beta1.zip
    2. using VS 2010 I created new Silverlight Application using RIA services
    3. added reference to nLog for silverlight
    4. adedd your code in App.xaml.cs and in MainPage.xaml.cs
    5. now when pressing the button no file is created in My Documents folder…

    What I am missing or nLog is not working at all?
    Btw, I could configure nlog for the Web project, but for the silverlight project I could not.
    Also I could not find in the internet no sample for nLog working with silverlight 4!
    I think this would be extremely helpful if there is some simple example
    Thanks in advance :)

  7. Jamie says:

    Based on the example above, you need to run it in OOB with elevated trust. Then you’ll see the log file created and updated in (this case) your “My Documents” folder.

Leave a Comment