xBIM 日志操作

Posted 在水一方

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xBIM 日志操作相关的知识,希望对你有一定的参考价值。

     目录

一、日志操作

       xBIM 使用Log4Net组件来处理错误和警告信息,同时也在xBIM 几何引擎中用于记录所有的几何错误信息,如果您发现你的几何或者模型无法加载,那么请查看日志。Log4Net 有很多配置,可以将日志输出到控制台、创建连续或滚动的日志文件。在程序解决方案配置文件 App.config,配置文件信息如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <!-- 定义默认日志操作 -->
    <root>
      <appender-ref ref="console" />
      <!-- 设置日志等级 ALL DEBUG INFO WARN ERROR FATAL NONE -->
      <level value="ALL" />
    </root>
    <appender name="console" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="FATAL" />
        <foreColor value="White" />
        <backColor value="Red" />
      </mapping>
      <mapping>
        <level value="ERROR" />
        <foreColor value="Red, HighIntensity" />
      </mapping>
      <mapping>
        <level value="WARN" />
        <foreColor value="Green, HighIntensity" />
      </mapping>
      <mapping>
        <level value="INFO" />
        <foreColor value="Blue, HighIntensity" />
      </mapping>
      <mapping>
        <level value="DEBUG" />
        <foreColor value="White" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5level - %message  [%logger %type.%method Line %line]%newline" />
      </layout>
    </appender>
  </log4net>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
</configuration>

      在开发过调试过程中可以使用控制台,但如果应用程序发布给客户电脑上,如果系统出现错误,有时候需要根据日志来筛查问题。那么我推荐使用以通过SMTP电子邮件传递日志。通过以电子邮件的形式发送到邮箱,那样下班后随时都可以查看发生的问题。当然这需要通过客户的同意,未经别人同意随意获取相关信息,,你懂得。。

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
    <to value="to@domain.com" />
    <from value="from@domain.com" />
    <subject value="test logging message" />
    <smtpHost value="SMTPServer.domain.com" />
    <bufferSize value="512" />
    <lossy value="true" />
    <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="WARN"/>
    </evaluator>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
    </layout>
</appender>
                

    在系统平时用的比较多的是文件记录或者数据库。

    MS SERVER 数据库配置如下:

// 在数据库中创建表
CREATE TABLE [dbo].[Log] (
    [Id] [int] IDENTITY (1, 1) NOT NULL,
    [Date] [datetime] NOT NULL,
    [Thread] [varchar] (255) NOT NULL,
    [Level] [varchar] (50) NOT NULL,
    [Logger] [varchar] (255) NOT NULL,
    [Message] [varchar] (4000) NOT NULL,
    [Exception] [varchar] (2000) NULL
)

     配置文件

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="100" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" />
    <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
    <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%thread" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%logger" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
</appender>

二、程序中使用

    使用xBIM 基础结构来记录日志消息,如下代码

using Xbim.Common.Logging;
var log = LoggerFactory.GetLogger(); 

log.Info("Examples are just about to start.");
log.Warn("Always use LINQ instead of general iterations!");
log.Error("This is how the error would be logged with log4net.");
log.Info("All examples finished.");

三、xBIM 日志高级使用

     xBIM 一个有趣的方面,就是整个设计核心中,模型的每一个变化都是通过事务来实现的。IModel并且从那里被弱引用,所以当你使用using语句模型,只要你保留它,只保留对事务的引用。这意味着有一个单一点,所有的变化正在发生,我们可以做一些事情。记录模型的所有变化,以前的状态和下一个状态,将这些所有的结合在一起,可以创建反向日志和向前日志,为了简化这个任务xBIM 实现了一个Xbim.IO.Delta.TransactionLog类。在下面的列子看我们如何使用:

using System;
using Xbim.Common;
using Xbim.Ifc;
using Xbim.Ifc4.Interfaces;
using Xbim.IO.Delta;
using Xbim.IO.Step21;
var editor = new XbimEditorCredentials
{
    ApplicationDevelopersName = "You", //应用开发商名称
    ApplicationFullName = "Your app",  //应用程序名称
    ApplicationIdentifier = "Your app ID", //应用程序标示符
    ApplicationVersion = "4.0", 
    //您的个人信息
    EditorsFamilyName = "lu",
    EditorsGivenName = "yongqiang",
    EditorsOrganisationName = "bim" 
};
using (var model = IfcStore.Open("SampleHouse.ifc", editor, true))
{
    using (var txn = model.BeginTransaction("Modification"))
    {
        using (var log = new TransactionLog(txn))
        {
            //修改墙的相关信息
            var wall = model.Instances.FirstOrDefault<IIfcWall>();
            wall.Name = "Unexpected name";
            wall.GlobalId = Guid.NewGuid().ToPart21();
            wall.Description = "New and more descriptive description";

            //打印变化的日志信息
            PrintChanges(log);
            txn.Commit();
        }
        Console.WriteLine();
    }
}
private static void PrintChanges(TransactionLog log)
{
    foreach (var change in log.Changes)
    {
        switch (change.ChangeType)
        {
            case ChangeType.New:
                Console.WriteLine(@"New entity: {0}", change.CurrentEntity);
                break;
            case ChangeType.Deleted:
                Console.WriteLine(@"Deleted entity: {0}", change.OriginalEntity);
                break;
            case ChangeType.Modified:
                Console.WriteLine(@"Changed Entity: #{0}={1}", change.Entity.EntityLabel, change.Entity.ExpressType.ExpressNameUpper);
                foreach (var prop in change.ChangedProperties)
                    Console.WriteLine(@"        Property \'{0}\' changed from {1} to {2}", prop.Name, prop.OriginalValue, prop.CurrentValue);
                break;
            default:
                break;
        }
    }
}

生成的更改日志如下:

Changed Entity: #1229=IFCWALL
        Property \'Name\' changed from \'Basic Wall:Wall-Ext_102Bwk-75Ins-100LBlk-12P:285330\' to \'Unexpected name\'
        Property \'OwnerHistory\' changed from #42 to #83873
        Property \'GlobalId\' changed from \'3cUkl32yn9qRSPvBJVyWw5\' to \'0zxW3$9z95n8U_H9YOcyiE\'
        Property \'Description\' changed from $ to \'New and more descriptive description\'
New entity: #83873=IFCOWNERHISTORY(#83876,#83877,$,.MODIFIED.,$,$,$,0);
New entity: #83874=IFCPERSON($,\'Santini Aichel\',\'Johann Blasius\',$,$,$,$,$);
New entity: #83875=IFCORGANIZATION($,\'Independent Architecture\',$,$,$);
New entity: #83876=IFCPERSONANDORGANIZATION(#83874,#83875,$);
New entity: #83878=IFCORGANIZATION($,\'You\',$,$,$);
New entity: #83877=IFCAPPLICATION(#83878,$,\'Your app\',\'Your app ID\');

 

以上是关于xBIM 日志操作的主要内容,如果未能解决你的问题,请参考以下文章

xBIM 插入复制功能

xBIM IFC 层次结构

XBIM IFC 墙壁案例

xBIM 多个IFC文件合并

xBIM IFC 输出 Excel 报表

xBIM 使用Linq 来优化查询