最近項目需要用log4net來寫日志,因為整個平台式在sharepoint上,我們需要記錄具體是哪個子站點發生的日志,因此需要再原來的log表裡面添加一個自定義信息列。由於平台的安全性要求,我們需要對連接字符串加密。連接字符串的信息是在AdoNetAppender讀取的,所以我們要擴展該類。而自定義消息我們需要擴展PatternLayout類。其實有關log4net的介紹網上已經很多了,如log4net.dll使用
具體的代碼如下:
#region Log helper public class LogMessage { public string CustomMessage { get; set; } public string Message { get; set; } public override string ToString() { return Message; } } internal class CustomMessageConverter : PatternLayoutConverter { protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent) { var logMessage = loggingEvent.MessageObject as LogMessage; if (logMessage != null) { writer.Write(logMessage.CustomMessage); } } } internal class CustomLayout : PatternLayout { public CustomLayout() { this.AddConverter("CustomMessage", typeof(CustomMessageConverter)); } } public class CustAdoNetAppender : AdoNetAppender { string _connectiongStr = string.Empty; public new string ConnectionString { set { _connectiongStr = value; base.ConnectionString = _connectiongStr; } get { return base.ConnectionString; } } } #endregion
配置代碼
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections> <log4net> <logger name="MyLogger"> <level value="ALL"></level> <appender-ref ref="ADONetAppender"></appender-ref> </logger> <!--記錄日志到數據庫--> <appender name="ADONetAppender" type="ConsoleApp.CustAdoNetAppender"> <bufferSize value="100" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <connectionString value="data source=.;initial catalog=test;integrated security=false;persist security info=True;User ID=sa;Password=xxx" /> <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[CustomMessage]) VALUES (@log_date, @thread, @log_level, @logger, @message,@customMessage)" /> <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="@customMessage" /> <dbType value="String" /> <size value="4000" /> <layout type="ConsoleApp.CustomLayout"> <conversionPattern value="%CustomMessage" /> </layout> </parameter> </appender> </log4net> </configuration>
SQL腳本如下:
CREATE TABLE [dbo].[Log] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [Date] [datetime] NOT NULL , [Thread] [varchar] (255) NOT NULL , [Level] [varchar] (20) NOT NULL , [Logger] [varchar] (255) NOT NULL , [Message] [varchar] (4000) NOT NULL , [CustomMessage] VARCHAR(max) NULL ) ON [PRIMARY]
在AssemblyInfo.cs文件中添加一句
[assembly: log4net.Config.DOMConfigurator(ConfigFile = "Log4Net.xml", Watch = true)]
調用代碼:
ILog log = LogManager.GetLogger("MyLogger");
log.Info(new LogMessage { Message = "message", CustomMessage = "cust" });
運行結果:
你是不是沒有找到連接字符串?一般在config擴展名的文本文件中,搜索看看吧。
一般是這樣寫的
Data Source=.;Initail Catalog=數據庫;UID=用戶名;PWD=密碼;
使用web.config的理由就是 有一些設置會經常需要更改 在不更改程序代碼的情況下 由web.config來定義參數是最好的
<connectionStrings>
<add name="項目名" connectionString="Data Source=你要設置的IP(如果是SQL2005或以上 或者一台機器上裝有多個版本的SQL 需要在IP後加SQL實例名);User ID=用戶名;Password=密碼;persist security info=False;initial catalog=數據庫名;" providerName="System.Data.SqlClient" />
</connectionStrings>