SP2013:SPDIagnosticsServiceBase.GetLocal

时间:2017-11-20 17:00:05

标签: c# exception sharepoint sharepoint-2013

我在SharePoint 2013服务器场上创建了一个自定义诊断服务,扩展了SPDiagnosticsServiceBase类。

using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Custom.Sharepoint.Services.Logging
{
    [System.Runtime.InteropServices.Guid("DBEEB5AB-C5A7-46B5-A2BB-5581F960C333")]
    public class CustomLoggingService : SPDiagnosticsServiceBase
    {
        public static string CustomLoggingServiceName = "Custom Logging Service";
        public static string CustomLoggingServiceTypeName = "Custom Logs";

        public enum CategoryId
        {
            General = 0,
            Base = 100,
            QA = 200,
            Admin = 300
        }

        public CustomLoggingService()
        {

        }

        public CustomLoggingService(string name, SPFarm farm) : base(CustomLoggingServiceName, farm)
        {

        }

        public static CustomLoggingService Local
        {
            get
            {
                return GetLocal<CustomLoggingService>();
            }
        }

        public override string DisplayName
        {
            get
            {
                return CustomLoggingServiceName;
            }
        }

        public override string TypeName
        {
            get
            {
                return CustomLoggingServiceTypeName;
            }
        }

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsCategory> categories = new List<SPDiagnosticsCategory>();
            foreach (string catName in Enum.GetNames(typeof(CategoryId)))
            {
                uint catId = (uint)(int)Enum.Parse(typeof(CategoryId), catName);
                categories.Add(new SPDiagnosticsCategory(catName, TraceSeverity.Verbose, EventSeverity.Error, 0, catId));
            }

            yield return new SPDiagnosticsArea(CustomLoggingServiceName, categories);
        }
        public SPDiagnosticsCategory this[CategoryId id]
        {
            get
            {
                return Areas[CustomLoggingServiceName].Categories[id.ToString()];
            }
        }

        public static void LogError(CategoryId category, string errorMessage, TraceSeverity severity)
        {
            SPDiagnosticsCategory cat = Local.Areas[CustomLoggingServiceName].Categories[category.ToString()];
            Local.WriteTrace(0, cat, severity, errorMessage);
        }
    }
}

在LogError方法中使用Local时,GetLocal方法会抛出以下异常:

  

Microsoft.SharePoint.Administration.SPDuplicateObjectException:Custom.Sharepoint.Services.Logging.CustomLoggingService类型的对象,名为&#34; Custom Logging Service&#34;已存在于名为&#34; SharePoint_Config_825b764b-31e1-4f9e-a722-558c56a5b0af&#34;的父Microsoft.SharePoint.Administration.SPFarm下。重命名对象或删除现有对象   在Microsoft.SharePoint.Administration.SPConfigurationDatabase.StoreObject(SPPersistedObject obj,Boolean storeClassIfNecessary,Boolean ensure)
  在Microsoft.SharePoint.Administration.SPConfigurationDatabase.Microsoft.SharePoint.Administration.ISPPersistedStoreProvider.PutObject(SPPersistedObject persistedObject,Boolean ensure)
  在Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate()
  在Microsoft.SharePoint.Administration.SPDiagnosticsServiceBase.Update()
  在Microsoft.SharePoint.Administration.SPDiagnosticsServiceBase.GetLocalToFarm [T](SPFarm场)
  在Custom.Sharepoint.Services.Logging.CustomLoggingService.get_Local()
  at Custom.Sharepoint.Services.Logging.CustomLoggingService.LogError(CategoryId category,String errorMessage,TraceSeverity severity)
  在Custom.Sharepoint.QA.Features.QAProbe.QAProbeEventReceiver.logToULS(字符串消息,TraceSeverity严重性)
  在Custom.Sharepoint.QA.Features.QAProbe.QAProbeEventReceiver.FeatureActivated(SPFeatureReceiverProperties属性)
  在Microsoft.SharePoint.SPFeature.DoActivationCallout(布尔fActivate,布尔fForce)
  在Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent,SPWeb webParent,SPFeaturePropertyCollection props,SPFeatureActivateFlags activateFlags,Boolean fForce)

这里棘手的部分是,只有在从FeatureActivated事件或Timer Job调用LogError时才会出现此异常。如果它是从网站部分调用的,我没有任何问题。而这只发生在一定时间之后。我确实安装了最新的SP更新并在服务器场上运行了PSConfigUI。我不能说这是确切的原因。除此之外,我只在Dev环境中遇到这个问题,但现在我不想在prod上部署任何更新,并打破一切。

Exception显示GetLocal方法调用GetLocalToFarm方法,但我尝试直接使用它,并得到相同的结果。

我认为GetLocal实际上会获得现有对象的句柄,但根据例外,它看起来像是在尝试创建它,因为它已经存在它会抛出异常...我需要使用本地对象,因为我想使用Logging服务,因此我们的管理员可以在CA中管理捕获的日志级别。 (我的自定义日志服务确实出现在CA的诊断日志记录配置中)

我使用这段代码记录来自其他定制解决方案的任何部分的错误消息:

CustomLoggingService.LogError(CustomLoggingService.CategoryId.QA, "Message from the QA Solution in the ULS", TraceSeverity.High);

由于

0 个答案:

没有答案