xBIM 基础04 日志操作

  xBIM使用Log4Net来记录异常不适合的处理错误和警告。它在xBIM几何引擎中被大量使用来记录所有几何错误。如果您的文件看起来不太正确,则应始终先检查日志。Log4Net有许多可能的配置,包括记录到控制台,创建连续或滚动日志文件等。这里可以找到一些例子。在控制台中编写所有彩色日志消息的最基本配置可能如下所示:

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3   <configSections>
 4     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
 5   </configSections>
 6   <log4net>
 7     <!-- 定义默认日志操作 -->
 8     <root>
 9       <appender-ref ref="console" />
10       <!-- 设置日志等级 ALL DEBUG INFO WARN ERROR FATAL NONE -->
11       <level value="ALL" />
12     </root>
13     <appender name="console" type="log4net.Appender.ColoredConsoleAppender">
14       <mapping>
15         <level value="FATAL" />
16         <foreColor value="White" />
17         <backColor value="Red" />
18       </mapping>
19       <mapping>
20         <level value="ERROR" />
21         <foreColor value="Red, HighIntensity" />
22       </mapping>
23       <mapping>
24         <level value="WARN" />
25         <foreColor value="Green, HighIntensity" />
26       </mapping>
27       <mapping>
28         <level value="INFO" />
29         <foreColor value="Blue, HighIntensity" />
30       </mapping>
31       <mapping>
32         <level value="DEBUG" />
33         <foreColor value="White" />
34       </mapping>
35       <layout type="log4net.Layout.PatternLayout">
36         <conversionPattern value="%-5level - %message  [%logger %type.%method Line %line]%newline" />
37       </layout>
38     </appender>
39   </log4net>
40   <startup>
41     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
42   </startup>
43 </configuration>

您还可以使用xBIM基础结构来记录您自己的消息,如下所示:

1 using Xbim.Common.Logging;
2 
3 
4 var log = LoggerFactory.GetLogger(); 
5 
6 log.Info("Examples are just about to start.");
7 log.Warn("Always use LINQ instead of general iterations!");
8 log.Error("This is how the error would be logged with log4net.");
9 log.Info("All examples finished.");
 
posted @ 2019-05-31 16:43  张传宁  阅读(511)  评论(0编辑  收藏  举报
页脚 HTML 代码