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

  • 相关阅读:
    C# decimal保留指定的小数位数,不四舍五入
    C# :实现水印与图片合成,并利用Graphics 压缩图像质量 , (委托实现listBox的动态添加提示)
    手机游戏模拟器汇总 用于开发
    WinAPI 操作串口
    C#图片压缩算法
    SQL SERVER 2008 无法启动TSQL调试的解决方法
    C#放缩、截取、合并图片并生成高质量新图的类
    C#图片处理之: 另存为压缩质量可自己控制的JPEG
    URL及short URL短网址
    1的补码及2的补码
  • 原文地址:https://www.cnblogs.com/ahghy/p/2874883.html
Copyright © 2011-2022 走看看