zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Linux学习之十九-Linux磁盘管理
    Linux学习之十八-sudo分权管理
    Linux学习之十七-配置Linux简单的脚本文件自启动
    Linux系统救援模式应用:恢复误删的系统文件
    Linux学习之十六-Linux用户管理
    Linux学习之十五-Linux文件特殊权限和附加权限
    Linux学习之十四-Linux文件和目录权限
    Linux学习之十三-vi和vim编辑器及其快捷键
    Linux学习之十二-Linux文件属性
    Linux系统救援模式应用:单用户模式找回密码
  • 原文地址:https://www.cnblogs.com/ahghy/p/2874883.html
Copyright © 2011-2022 走看看