• 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

  • 相关阅读:
    八、UIViewController们之间的协作——Segue
    七、UIViewController导航栏
    六、APP开发的主角——UIViewController
    五、UI开发之核心基础——约束(深入)
    四、UI开发之核心基础——约束(实用)
    三、UI开发之核心基础——约束(入门)
    iOS开发笔记错误集
    Unity中内嵌网页插件UniWebView使用总结
    利用Aspose.Word控件实现Word文档的操作
    ReSharper 配置及用法(ZHUANG)
  • 原文地址:https://www.cnblogs.com/ahghy/p/2874883.html
Copyright © 2020-2023  润新知