SharePoint Framework web part 使用log API记录log

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shrenk/article/details/88693788

SharePoint Framework 提供了Log API,用来在浏览器的控制台输出web part的log。如果需要使用这个Log API,首先需要导入Log对象:

import { Log } from '@microsoft/sp-core-library';

Log对象提供了四种静态log方法,对应四种log级别:

//信息
Log.verbose(source: string, message: string, scope?: ServiceScope): void
//详细信息
Log.info(source: string, message: string, scope?: ServiceScope): void
//警告信息
Log.warn(source: string, message: string, scope?: ServiceScope): void
//错误信息
Log.error(source: string, error: Error, scope?: ServiceScope): void

其中第一个参数source,指明log信息的来源,通常是webpart或者类的名字,最长20个字符。

第二个参数是log信息,其中error方法中的第二个参数需要构造一个Error对象,除了记录错误信息之外还会在控制台输出堆栈信息。

第三个参数是可选的ServiceScope对象,这个对象可以从webpart的context中获得,在log中传入这个参数会输出更详细的webpart上下文的信息。

代码示例:

Log.info('HelloWorld', 'this is information log', this.context.serviceScope);
Log.verbose('HelloWorld', 'this is verbose log', this.context.serviceScope);
Log.warn('HelloWorld', 'this is warn log', this.context.serviceScope);
Log.error('HelloWorld', new Error('this is error log'), this.context.serviceScope);

如果使用serviceScope,会输出更详细的webpart信息

如果不使用serviceScope,只输出log信息:

参考文档:https://github.com/SharePoint/sp-dev-docs/wiki/Working-with-the-Logging-API
 

猜你喜欢

转载自blog.csdn.net/shrenk/article/details/88693788