How to: Add logging to a Lync Controls application
Learn how to add logging to a Microsoft Windows Presentation Foundation (WPF) application that contains Microsoft Lync Controls. The application writes log entries to a file on the local computer when the Initialized event on the PresenceIndicator control occurs.
Applies to: Lync 2013 | Lync Server 2013
In this article |
![]() |
---|
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. |
Prerequisites
For a list of prerequisites, see How to: Create a Silverlight page that displays a Lync presence control.
Create a WPF application
To create the WPF application
Create a Lync WPF application.
For more information, see How to: Create a Silverlight page that displays a Lync presence control.
In Window1.xaml, add a declaration for the Initialized event to the PresenceIndicator control.
<controls:PresenceIndicator Initialized="PresenceEventHandler" Source="sip:elise@contoso.com"/>
In the following procedures, 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
In Window1.xaml.cs, add the following two using directives.
using Microsoft.Lync.Utilities.Logging; using System.IO;
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) { w.WriteLine(""); w.WriteLine(logEntry.Message); w.Flush(); } }
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
In Window1.xaml, right-click the Initialized event declaration, and then click Navigate to Event Handler.
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."; Logger.Write(myEntry); }
Debug the application
Run the application and then browse to the text file on the local computer and review the log entry.
To see logging in action
Press F5 to build and run the application.
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'