csharp 带有自定义分类和消息的SharePoint 2010自定义日志条目

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 带有自定义分类和消息的SharePoint 2010自定义日志条目相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace RDR_2010.RDR
{
    class ULSLog2010 : SPDiagnosticsServiceBase
    {
        public const string PRODUCT_NAME = "eRDR";
        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.Medium, message);
        }

        public static void LogDebug(string message)
        {
            SPDiagnosticsCategory category = ULSLog2010.Current.Areas[PRODUCT_NAME].Categories["Debugging"];
            ULSLog2010.Current.WriteTrace(1, category, TraceSeverity.Medium, message);
        }

    }

}

以上是关于csharp 带有自定义分类和消息的SharePoint 2010自定义日志条目的主要内容,如果未能解决你的问题,请参考以下文章

带有 HTTPStatusCodeResult 和 jQuery 的自定义错误消息

带有自定义按钮的 ExtJs 消息框

自定义发布类型分类法和消息

带有错误代码和错误消息的自定义 Python 异常

带有翻译器消息的 Api 平台自定义异常

已发送邮件不显示带有自定义 Outlook 消息表单的邮件跟踪