Home Diagnostics and logging Reading and writing SharePoint logs

Reading and writing SharePoint logs

In this posting I will introduce internals of SharePoint logs. You will get overview of logging background and I suggest some tools to browse and analyze log files. I will also introduce some tricks how to send messages to log from your own web parts and other extensions. Each topic here has also rich set of links that help you go further.

Unified Logging System

WSS 3.0 and MOSS 2007 both use Unified Logging System (ULS) to write messages to SharePoint log. ULS uses Event Tracing for Windows (ETW). This is logging and tracing technology first introduced in Windows 2000. ETW is available through Windows API and it should be very efficient logging technology.

Although ULS is powerful technology it is not easy to use without third-party tools. You can configure some settings in SharePoint Central Administration site but there are no default tools that make reading the logs easy.

References

How to read log files

You can use different tools to read and analyze SharePoint log files. All log file reading tools introduced here are third-party tools from CodePlex and other sites. They work nice and if you find something you like you can try it.

Starznet WSS / MOSS Log File Reader

Starznet WSS / MOSS Log File Reader is SharePoint solution that you can install to Central Administration web site. It adds new “Log File Viewer” selection under “Logging and Reporting” block. Following screenshot gives you some idea about what to expect. Most useful feature that Starznet offers is log files search. I know what a pain can be searching and reading log files using Notepad or some other simple text editor.

Starznet Log File Viewer
Starzner WSS / MOSS Log File Reader. Screenshot shows file contents view.
Click on image to enlarge.

I got it installed without any problems and it works nice. I found also one problem – Log File Reader is not able to read current log file because of file locks. 

SPTraceView

SPTraceView notifying about new message If you need something very simple to monitor your logs in real-time, you can use SPTraceView utility by Hristo Pavlov. SPTraceView also offers table view of active log file and you can open it by double clicking on trace icon.

SPTraceView has also settings dialog where you can modify logging level and message filters. It is also possible to send selected messages to DebugView. I think SPTraceView may be useful tool for developers to track problems in test environment. It is faster than monitoring log files manually.

Microsoft Log Parser 2.2

Microsoft Log Parser is command-line tool you can use to analyze ULS log files. Log Parser lets you execute queries against ULS log files and save output to another text file. Performance of this tool is pretty good. MSDN blog entry ULS Log File Data Mining gives example of query that uses aggregate functions and that handles almost 1.5 million log lines with 77 seconds.

You can use this tool to execute your own SQL-like queries against log files to get out information you really need. There is one gotcha – Log Parser doesn’t know that log messages in ULS logs may take more than one line.

References

How to write to log files

Writing to SharePoint log files from your is simple if your code has to run only on MOSS 2007. It takes only one line of code.


Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Logged!");


That’s it. But if you are using WSS or you want logging solution that works for both of them, you need third-party libraries. Of course, you can write your own logging solution if you have time enough. There is third-party library called SharePoint.Diagnostics that let’s you do something like this.


using System;

using System.Runtime.InteropServices;

using System.Web.UI;

 

using Microsoft.SharePoint.WebPartPages;

using SharePoint.Diagnostics;

 

namespace TestWebParts

{

    [Guid("2d2e82e3-8f47-4764-1236-b1c06f104183")]

    public class LoggingWebPart : WebPart

    {

        /// <summary>

        /// Create all your controls here for rendering.

        /// Try to avoid using the RenderWebPart() method.

        /// </summary>

        protected override void CreateChildControls()

        {

            try

            {

                Trace.LogMessage("This message goes to SharePoint log!",

                                TraceSeverity.High,"LoggingWebPart");

                Controls.Add(new LiteralControl("No errors!"));

            }

            catch(Exception ex)

            {

                Controls.Add(new LiteralControl(ex.ToString()));

            }

        }

    }

}


There are some tricks to make it work. After downloading WSP change its extension to cab. Extract SharePoint.Diagnostics.dll and add reference to it to your project.

References

Epilogue

ULS logs are powerful sources of information. Reading and writing SharePoint logs lies heavily on third-party and custom solutions right now. With free and open-source third-party tools it is easy to read and write SharePoint logs. Developers can easily write messages of their extensions to SharePoint logs and this makes extensions to fit better in SharePoint environment.

  1. No comments yet.
  1. No trackbacks yet.
WordPress SEO fine-tune by Meta SEO Pack from Poradnik Webmastera