博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SharePoint 2010 自定义日志
阅读量:6501 次
发布时间:2019-06-24

本文共 4814 字,大约阅读时间需要 16 分钟。

(Clean Debugging and Error Logging broken down into steps)

By: Philip Stathis

原文地址

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 ( ) 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/archive/2013/01/24/2874883.html

你可能感兴趣的文章
背锅侠逆袭之路
查看>>
演示:使用协议分析器取证IPv6的报文结构
查看>>
oracle 11gr2 rac中的4种IP解说
查看>>
为什么你找不到工作?
查看>>
20 个免费的 jQuery 的工具提示插件:
查看>>
只有在北方的中国帝国能力享受免费的商业课程:财富规划法与愿景
查看>>
汇编语言的应用
查看>>
device platform 相应的表
查看>>
php des 加密解密实例
查看>>
【Mac】Mac键盘实现Home, End, Page UP, Page DOWN
查看>>
实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求
查看>>
安德鲁斯----多媒体编程
查看>>
swift版的元组
查看>>
[zz]在linux中出现there are stopped jobs 的解决方法
查看>>
Delphi下实现全屏快速找图找色 一、数据提取
查看>>
查询表字段信息
查看>>
logback与Log4J的区别
查看>>
关于机器学习的最佳科普文章:《从机器学习谈起》
查看>>
咏南新CS三层开发框架
查看>>
dxFlowChart运行时调出编辑器
查看>>