SharePoint 2010 自定义日志

How to log to the SharePoint ULS Logs

(Clean Debugging and Error Logging broken down into steps)

By: Philip Stathis

原文地址 http://www.thesharepointblog.net/Lists/Posts/Post.aspx?ID=122

This article is meant to introduce a simple error logging routine that can really simplify your debugging when needed.

I am assuming for this post that you know that SharePoint has ULS logs and that there is a nice tool called ULSViewer (http://archive.msdn.microsoft.com/ULSViewer ) that you can use to examine errors.

So let’s get to it, step by step process of spitting statements to ULS.

You need to create a new class anywhere in your project, and it can be called ULSLog2010.cs .

I pasted the code that you need to place in the class below, this is not a piece I authored myself but I can vouch for the results and ease of use.

Here’s what you need to do:

1.       Create a new class called ULSLog2010.cs

2.       Paste code below code in

3.       Replace GWStandard.Logging with the namespace that your code is using to make it available where you need it

4.       Replace SharePointCustomSolution with the name of your desired product.

5.       Use the code to record debugging statements or errors like so:

ULSLog2010.LogDebug("I am a debugging string");

//Input Error ex from catch

catch (Exception ex)

   {ULSLog2010.LogError(ex);}

6.       Filter ULS log by Product = SharePointCustomSolution (or the custom name)

7.       Cake

using System;

using System.Collections.Generic;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

namespace GWStandard.Logging

{

    /// <summary>

    /// Used for logging into Uls in 2010

    /// </summary>

    public class ULSLog2010 : SPDiagnosticsServiceBase

    {

        public const string PRODUCT_NAME = "SharePointCustomSolution";

        private static ULSLog2010 _Current;

       

        public static ULSLog2010 Current

        {

            get

            {

                if (_Current == null)

                {

                    _Current = new ULSLog2010();

                }

                return _Current;

            }

        }

        private ULSLog2010()

            : base(PRODUCT_NAME, SPFarm.Local)

        {

        }

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()

        {

            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>       

            {           

                new SPDiagnosticsArea(PRODUCT_NAME, new List<SPDiagnosticsCategory>           

                {               

                    new SPDiagnosticsCategory("Error", TraceSeverity.High, EventSeverity.Error),

                    new SPDiagnosticsCategory("Warning", TraceSeverity.Medium, EventSeverity.Warning),

                    new SPDiagnosticsCategory("Logging", TraceSeverity.Verbose, EventSeverity.Verbose),

                    new SPDiagnosticsCategory("Debugging", TraceSeverity.Verbose, EventSeverity.Verbose)

                })       

            };

            return areas;

        }

        private string MapTraceSeverity(TraceSeverity traceSeverity)

        {

            switch (traceSeverity)

            {

                case TraceSeverity.High: return "Error";

                case TraceSeverity.Medium: return "Warning";

                default:

                case TraceSeverity.Verbose:

                    return "Debugging";

            }

        }

        public static void Log(TraceSeverity traceSeverity, Exception ex)

        {

            SPDiagnosticsCategory category = ULSLog2010.Current.Areas[PRODUCT_NAME].Categories["Error"];

            ULSLog2010.Current.WriteTrace(0, category, TraceSeverity.High, ex.Message);

            ULSLog2010.Current.WriteTrace(0, category, TraceSeverity.High, ex.ToString());

        }

        public static void Log(TraceSeverity traceSeverity, string message, Exception ex)

        {

            SPDiagnosticsCategory category = ULSLog2010.Current.Areas[PRODUCT_NAME].Categories["Error"];

            ULSLog2010.Current.WriteTrace(0, category, TraceSeverity.High, ex.Message);

            ULSLog2010.Current.WriteTrace(0, category, TraceSeverity.High, ex.ToString());

        }

        public static void LogError(Exception ex)

        {

            SPDiagnosticsCategory category = ULSLog2010.Current.Areas[PRODUCT_NAME].Categories["Error"];

            ULSLog2010.Current.WriteTrace(0, category, TraceSeverity.High, ex.Message);

            ULSLog2010.Current.WriteTrace(0, category, TraceSeverity.High, ex.ToString());

        }

        public static void LogError(Exception ex, string message)

        {

            SPDiagnosticsCategory category = ULSLog2010.Current.Areas[PRODUCT_NAME].Categories["Error"];

            ULSLog2010.Current.WriteTrace(0, category, TraceSeverity.High, ex.Message);

            ULSLog2010.Current.WriteTrace(0, category, TraceSeverity.High, ex.ToString());

        }

        public static void LogError(string message, string stackTrace)

        {

            SPDiagnosticsCategory category = ULSLog2010.Current.Areas[PRODUCT_NAME].Categories["Error"];

            ULSLog2010.Current.WriteTrace(0, category, TraceSeverity.High, message);

        }

        public static void LogWarning(string message)

        {

            SPDiagnosticsCategory category = ULSLog2010.Current.Areas[PRODUCT_NAME].Categories["Warning"];

            ULSLog2010.Current.WriteTrace(1, category, TraceSeverity.Medium, message);

        }

        public static void LogMessage(string message)

        {

            SPDiagnosticsCategory category = ULSLog2010.Current.Areas[PRODUCT_NAME].Categories["Logging"];

            ULSLog2010.Current.WriteTrace(1, category, TraceSeverity.Verbose, message);

        }

        public static void LogDebug(string message)

        {

            SPDiagnosticsCategory category = ULSLog2010.Current.Areas[PRODUCT_NAME].Categories["Debugging"];

            ULSLog2010.Current.WriteTrace(1, category, TraceSeverity.Verbose, message);

        }

    }

}

By: Philip Stathis

原文地址:https://www.cnblogs.com/ahghy/p/2874883.html