7/6/2012

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

转载于:https://www.cnblogs.com/ahghy/archive/2013/01/24/2874883.html

SharePoint 2010 自定义日志相关推荐

  1. SharePoint 2010 ——自定义上传页面与多文件上传解决方案

    SharePoint 2010 --自定义上传页面与多文件上传解决方案 参考文章: (1)SharePoint 2010 --自定义上传页面与多文件上传解决方案 (2)https://www.cnbl ...

  2. SharePoint 2010自定义母版页小技巧——JavaScript和CSS引用

    通常在我们的项目中,都会涉及到母版页的定制.并且必不可少的,需要配合以一套自己的JavaScript框架和CSS样式. 你有没有遇到过这样的情况呢,在开发环境和UAT时都还算顺利,但是当最终部署到生产 ...

  3. SharePoint 2010 自定义Ribbon实现文档批量下载为Zip文件

    在SharePoint 2010文档库中,结合单选框,在Ribbon中提供了批量处理文档的功能,比如,批量删除.批量签出.批量签入等,但是,很遗憾,没有提供批量下载,默认的只能一个个下载,当选择多个文 ...

  4. SharePoint 2010文档库批量下载文档的实现

    在SharePoint 2010文档库中,结合单选框,在Ribbon中提供了批量处理文档的功能,比如,批量删除.批量签出.批量签入等,但是,很遗憾,没有提供批量下载,如图: 若选中多个文档后,会发现D ...

  5. SharePoint 2010 主题

    从SharePoint 2003开始,主题就作为一种自定义SharePoint外观的重要选项.SharePoint 2003和2007中的包括一系列开箱即用的主题,可以被网站所有者通过SharePoi ...

  6. SharePoint 2010 使用自定义aspx页面替换列表默认的新建(NewForm.aspx),查看(DispForm.aspx)和编辑(EditForm.aspx)页面...

    如何使用自定义的aspx页(比如Application Page)替换列表默认的NewForm.aspx, DispForm.aspx 和 EditForm.aspx的页面?如果你只是更改这些页面的界 ...

  7. 基于Windows Azure 搭建基于SharePoint 2010 Intranet、Extranet、Internet (1): 安装SharePoint 2010...

    4月底的时候,参加了微软的Windows Azure 体验活动,拿到了Azure的体验账号.经过一番体验后,发现Windows Azure确实是一个学习和实验的好平台.Windows Azure很快就 ...

  8. 简介SharePoint 2010 14 Hive文件夹

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u012025054/article/details/36018873 简介SharePoint 20 ...

  9. SharePoint 2010中的客户端AJAX应用——ASP.NET AJAX模板

    WCF Data Services是SharePoint 2010中一个极具吸引力的新特性.然而,因为它的强大,直接对其进行编程仍然会有点痛苦.幸运的是,一个新的相关技术 -- ASP.Net AJA ...

最新文章

  1. 用JavaScript来实现链表LinkedList
  2. 请教context:component-scan/和mvc:annotation-driven/的区别20
  3. 习题2.5 两个有序链表序列的合并 (15 分)
  4. 《机器学习实战》chapter05 Logistic回归
  5. html4的语法,HTML——语法
  6. Wireshark(1):Wireshark基本用法
  7. 方法性能分析器--装饰者模式应用
  8. python 加快计算速度_python怎么提高计算速度
  9. 软件调试中的断点分类
  10. 实战:一文带你解决Mysql主从复制日常错误
  11. C#部分类与部分方法
  12. github速成手册
  13. linux刷显卡bios版本,一种Linux系统下显卡刷新BIOS的方法与流程
  14. BJT与MOSFET与IGBT的区别
  15. mac如何设置默认输入法
  16. Windows 8激活产品密匙公布
  17. 海康萤石云 H5移动端和PC端云播放本地监控摄像头
  18. 第四次产业革命源于人工智能(趋势解读20k字)
  19. 宝塔如何安装多版本php,安装Lnmp(多PHP版本与宝塔)
  20. 有钱人抢豪宅,普通人不敢消费:社会在割裂,富人更富,穷人更穷

热门文章

  1. OSI[七层]与TCP/IP[四层]模型简述简图
  2. 在ASP.NET中如何用C#.NET实现基于表单的验证
  3. ASP.NET管理状态的十种途径
  4. 使用 mkdocs 搭建个人 wiki 站点
  5. 【Qt】Linux上设置自启动后qApp->applicationDirPath()的返回值问题
  6. 【leetcode】力扣刷题(3):无重复字符的最长子串(go语言)
  7. 【Qt】pro 笔记
  8. 有n个学生选修了c语言程序设计这门课程,C语言程序设计报告学生选修课系统(18页)-原创力文档...
  9. 【二级java】二叉树序列
  10. 【数据库】兴唐第二十八节课零散知识点汇总