Click here to Skip to main content
11,927,006 members (50,065 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked

Logging to Event Viewer with DiagnosticsLevel - SharePoint 2007

, 19 Dec 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Using the DiagnosticsLevel to decide on logging levels for custom webparts
The most least talked about is how to get the current DiagnosticsLevel of the "Areas" in SharePoint 2007. Unlike SharePoint 2010, finding the current DiagnosticsLevel in SharePoint 2007 is a bit tedious.

If you want to decided on your web-part logging level(s) without registering a custom area, following is the easy way:

public static void LogMessage(String errormsg, EventLogEntryType eventType, int eventID, short category)
        SPDiagnosticsService dsg = SPDiagnosticsService.Local;
        // Get the current levels.
        IEnumerable<IDiagnosticsLevel> levels = dsg.GetItems();
        foreach (IDiagnosticsLevel level in levels)
            if (level.Name == "WebParts")
                switch (level.EventSeverity)
                    case EventSeverity.Error:               // Error : Information / Warning / Errors
                    case EventSeverity.None:                // Do not log anything
                    case EventSeverity.Warning:             // Warning : Information
                        if (eventType == EventLogEntryType.Error) return;
                    case EventSeverity.Information:
                        if (eventType != EventLogEntryType.Information) return;
        if (!EventLog.SourceExists("MyEventSource"))
            EventLog.CreateEventSource("MyEventSource", "MyAppLogs");
        EventLog.WriteEntry("MyEventSource", errormsg, eventType, eventID, category);

This is a static method; so add it to your Web-Part and you can start logging.

Enjoy coding.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Adhish Pendharkar
Architect AdhiMarg Technologies Ltd.
Hong Kong Hong Kong
Resident of Hong Kong, TOGAF 8 certified architect with over 15 years of international experience in the IT industry. Exceptional exposure to the marine / shipping technologies and knowledge of large scale database system architecture, enterprise systems design and work flow implementation.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 19 Dec 2010
Article Copyright 2010 by Adhish Pendharkar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid