K2 BlackPearl的日志提供了多种方式来记录K2的信息,日志配置在C:\Program Files\K2 blackpearl\Host Server\Bin\HostServerLogging.config中,打开配置文件,可以看到下面的片段:











Name对应此config文件的Extensions节点中的Extension节点的Name属性,在下面可以看到。
Active控制是否使用此配置项,因此我们可以看出,默认情况下,K2只使用“ConsoleExtension”,也就是控制台模式。
LogLevel控制日志的级别。为了看到更详细的信息,我们调节这个属性来实现,从简单到详细依次是Warning->Error->Info->Debug->All
K2的调试:
在管理工具-服务中将K2服务停止,然后点击开始菜单的K2 blackpearl-K2 Server,此时K2会以控制台模式来启动服务,K2的相关信息就可以方便的看到,在我们的代码中使用Console.WriteLine("message") 可以方便的输出到这里。
另外使用命令行"C:\Program Files\K2 blackpearl\Host Server\Bin\K2HostServer.exe" > c:\k2info.txt 可以将信息输出到c:\k2info.txt
K2除了默认的集中记录日志的方式外,还让我们自己扩展实现自定义的记录方式,比如下面的片段:
















我们可以看到,这里有两个ApplicationLevelLogSetting节点,不同的是两个ApplicationLevelLogSetting的Scope不同。具体情形我还没有具体尝试,抱歉,因为这样的扩展实现的效果非常有限。下面具体来说下另外一种方式的扩展:
在HostServerLogging.config,找到Extensions节点,可以看到这样的配置:


















在这里我们可以清楚的看到第一段XML中的ConsoleExtension,FileExtension,EventLogExtension等节点的Name是从哪里来的了。在这里我们来添加自己的扩展配置,例如发送Email。首先我们编写自定义扩展的代码,继承BaseExtension,只需重写一个方法:


























看得出来,非常简单 :)
然后在上面所示的Extensions节点中加入自定义扩展的一个节点
type是我们扩展代码dll的namespace.class,location是这个dll的所在位置。
对应着LogLocationSettings节点中也加入一个节点,注意两个Name要匹配.
最后保存config文件,一个日志记录的扩展设置就OK了。