CHUVASH.eu

CHunky Universe of Vigourous Astonishing SHarepoint :)

A simple Log for ULS

Here is a simple log which has been inspired of Android Log. It logs to ULS which you can open with ULSViewer, SharePoint Log Viewer.

using System;
using Microsoft.SharePoint.Administration;

namespace Contoso.Intranet.Portal.Utilities
{
    public class Log
    {
        private static readonly string _CATEGORYNAME = "CONTOSO";
        private static readonly SPDiagnosticsCategory _ERROR_CATEGORY = 
              new SPDiagnosticsCategory(_CATEGORYNAME, TraceSeverity.Unexpected, EventSeverity.Error);
        private static readonly SPDiagnosticsCategory _WARNING_CATEGORY = 
              new SPDiagnosticsCategory(_CATEGORYNAME, TraceSeverity.High, EventSeverity.Warning);
        private static readonly SPDiagnosticsCategory _VERBOSE_CATEGORY = 
              new SPDiagnosticsCategory(_CATEGORYNAME, TraceSeverity.Verbose, EventSeverity.Verbose);
        private static readonly SPDiagnosticsCategory _INFO_CATEGORY = 
              new SPDiagnosticsCategory(_CATEGORYNAME, TraceSeverity.Medium, EventSeverity.Information);

        private static void WriteTrace(SPDiagnosticsCategory category, string message, string trace)
        {
            SPDiagnosticsService.Local.WriteTrace(0, category, category.DefaultTraceSeverity, message, trace);
        }

        public static void Error(Exception ex)
        {
            WriteTrace(_ERROR_CATEGORY, ex.Message, ex.StackTrace);
        }

        public static void Warning(string message)
        {
            WriteTrace(_WARNING_CATEGORY, message, "");
        }

        public static void Verbose(string message)
        {
            WriteTrace(_VERBOSE_CATEGORY, message, "");
        }

        public static void Info(string message)
        {
            WriteTrace(_INFO_CATEGORY, message, "");
        }

    }
}

A possible improvement can be a custom Area. See an example of ThorstenHans on Github: CustomLogger.cs

EDIT: I found an interesting article: How to log to the SharePoint ULS Logs: Clean Debugging and Error Logging broken down into steps written by Philip Stathis.

Advertisements

One response to “A simple Log for ULS

  1. Pingback: Do an unsafe update in a unified manner « Sharepoint. Kunskap. Upptäckter på resan.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager talking Office 365, SharePoint and Azure

My view and thoughts on Productivity and more.

My programming life

and everything in between

SharePoint Development Lab by @avishnyakov

It is a good place to share some SharePoint stories and development practices.

SharePoint Dragons

Nikander & Margriet on SharePoint

RealActivity - Real-time and trustworthy

Blog site of founder, RealActivty - Paul J. Swider

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Cameron Dwyer | Office 365, SharePoint, Outlook, OnePlace Solutions

Office 365, SharePoint, OnePlace Solutions & Life's Other Little Wonders

paul.tavares

Me and My doings!

Share SharePoint Points!!

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

SPJoel

SharePoint for everyone

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode, ya mama

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint

%d bloggers like this: