Log4Net 是一套專業的 Logger, 之前比較常處理的是記錄一般資料型態的紀錄,如 String, Datetime等資料。這篇將針對如何處理 Guid (在 SQL Server 為 uniqueidentifier 的資料型態)來做討論.
開發環境 VS 2012, Log4Net 透過 Nuget 安裝的 2.0.3 版本, 資料庫是 SQL Server 2008 R2
在 Web.Confg 中 ADoNetAppender 的設定, 其中 ORG_ID 在 DB 中是 uniqueidentifier
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<connectionString value="data source=localhost;initial catalog=LOCAL_DB;integrated security=false;persist security info=True;User ID=user;Password=password"/>
<commandText value="INSERT INTO SYS_EVENT_LOG ([DATE],[THREAD],[LEVEL],[LOGGER], [LINE], [MESSAGE],[EXCEPTION], [USER_NAME], [CREATE_USER], [IP], [MACHINE_NAME], [ACCESS_MODE], [ORG_ID]) VALUES (@log_date, @thread, @log_level, @logger, @line, @message, @exception, @user_name, @create_user, @ip, @machine_name, @access_mode, @org)"/>
<bufferSize value="1"/>
<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="@line"/>
<dbType value="String"/>
<size value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%line"/>
</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>
<parameter>
<parameterName value="@user_name"/>
<dbType value="String"/>
<size value="50"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{user_name}"/>
</layout>
</parameter>
<parameter>
<parameterName value="@create_user"/>
<dbType value="String"/>
<size value="50"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{create_user}"/>
</layout>
</parameter>
<parameter>
<parameterName value="@ip"/>
<dbType value="String"/>
<size value="30"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{ip}"/>
</layout>
</parameter>
<parameter>
<parameterName value="@machine_name"/>
<dbType value="String"/>
<size value="100"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{machine_name}"/>
</layout>
</parameter>
<parameter>
<parameterName value="@access_mode"/>
<dbType value="String"/>
<size value="1"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{access_mode}"/>
</layout>
</parameter>
<parameter>
<parameterName value="@org" />
<dbType value="Guid" />
<layout type="log4net.Layout.RawPropertyLayout">
<conversionPattern value="%property{org}" />
<key value="org" />
</layout>
</parameter>
</appender>
</log4net>
在 C#
User.BU = Guid.NewID(); // get a new GUID
log4net.GlobalContext.Properties["ip"] = user.LoginIP;
log4net.GlobalContext.Properties["machine_name"] = user.LoginIP;
log4net.GlobalContext.Properties["access_mode"] = "W";
log4net.GlobalContext.Properties["create_user"] = user.UserAccount;
log4net.GlobalContext.Properties["user_name"] = user.UserName;
log4net.GlobalContext.Properties["org"] = user.BU;
沒有留言:
張貼留言