Walkthrough: Add Logging to a Lync Controls Application

This walkthrough shows how to add logging to a Microsoft Windows Presentation Foundation (WPF) application that contains a Microsoft Lync 2010 Control. The walkthrough writes log entries to a file on the local computer when the Initialized event on the PresenceIndicator control occurs.


When logging is used in WPF or Microsoft Silverlight applications, personal information such as telephone numbers can appear in logs. To protect confidential information, ensure logs are written to secure locations.

In Silverlight applications, I/O operations typically are restricted to isolated storage and do not use the file system of the operating system. It is possible to work around this restriction by logging to a web service, or by using COM to access the local file system. For more information, see the MSDN articles Isolated Storage and CLR Inside Out: Isolated Storage In Silverlight 2.


For a list of prerequisites, see Walkthrough: Presence Hello World.

Creating the WPF Application

To create the WPF walkthrough application

  1. Create a Lync WPF application. For more information, see the topic Walkthrough: Presence Hello World.

  2. In Window1.xaml, add a declaration for the Initialized event to the PresenceIndicator control.

    <controls:PresenceIndicator Initialized="PresenceEventHandler" Source="sip:elise@contoso.com"/>

Add C# Code

In the following procedures, you add code to the Initialized event previously registered in the XAML text, implement a class derived from the LogListener class, implement the Write method of that class, and then add code to write entries to a file on the local computer.

To implement a LogListener class

  1. In Window1.xaml.cs, add the following two using directives.

    using Microsoft.Lync.Utilities.Logging;
    using System.IO;
  2. Implement a class derived from LogListener.

    // Implementation of the LogListener class
    class MyListener : LogListener
      FileStream fs;
      StreamWriter w;
      public MyListener()
        // Change the file path to a valid value
        fs = new FileStream(@"C:\Temp\log.txt", FileMode.Create);
        w = new StreamWriter(fs, Encoding.UTF8);
      public MyListener(LogLevel myLevel, string[] categories)
      { }
      public override void Write(LogEntry logEntry)

To add LogListener to the logging framework

  • In the Window1 constructor, add the following statements to instantiate a LogListener object and then add it to the Listeners collection.

    MyListener defaultListener = new MyListener();   //Instantiate a LogListener object.
    Logger.AddListener(defaultListener);   //Add it to the Listeners collection.
    Logger.Level = LogLevel.Verbose;   //Set a value indicating the event types to be logged.

To implement the Initialized event

  1. In Window1.xaml, right-click the Initialized event declaration and then click Navigate to Event Handler.

  2. In the event handler, add code to create a LogEntry object and then call the Write method on the Logger object.

    private void PresenceEventHandler(object sender, EventArgs e)
      LogEntry myEntry = new LogEntry(LogLevel.Verbose, null, null, “log text”);
      myEntry.Level = LogLevel.Verbose;
      myEntry.Message = "A test log entry.";

Debug the Application

Run the application and then on the local computer, browse to the text file and review the log entry.

To see logging in action

  1. Press F5 to build and run the application.

  2. Browse to the text file to view the log entry.

Sample Logging Output

Created CollectionViewSource
Populated ContactList Hash: 21551780
Got groups from internal model
Loaded ContactList Hash: 21551780
Control 'ContactItem' entering 'OnLoaded'
=================> CreateContact ContactItem, john@contoso.com
Entering GetContactOrGroupByUri<String>:john@contoso.com
Populated ContactItem Hash: 11207750
Loaded ContactItem Hash: 11207750
Control 'ContactItem' entering 'OnLoaded'
=================> CreateContact ContactItem, jane@contoso.com
Entering GetContactOrGroupByUri<String>:jane@contoso.com
Populated ContactItem Hash: 56864589
Loaded ContactItem Hash: 56864589
Control 'ContactSearchInputBox' entering 'OnLoaded'

See Also


Using Logging in Lync 2010 Controls

Walkthrough: Presence Hello World

Other Resources

How to use logging to troubleshoot Microsoft Lync Online installation issues

Routed Events Overview

Lync 2010 Controls Walkthroughs