十万火急Log4net 程序中配置数据库连接,项目紧急,感谢各位。。。

时间:2021-09-15 13:13:32
    如题,用log4net 将异常日志写到数据库中,我想在在程序中对Log4net的数据库连接进行设置,请问各位如何实现???

6 个解决方案

#1


<log4net> 

… 

<appender name="adonetappender" type="log4net.appender.adonetappender"> 

                    <buffersize value="10" /> 

                    <connectiontype value="system.data.sqlclient.sqlconnection, system.data, version=1.0.3300.0, culture=neutral, publickeytoken=b77a5c561934e089" /> 

                    <connectionstring value="server=192.168.1.112;database=domino;uid=sa;pwd=;timeout=300;" /> 

                    <commandtext value="insert into mail_web_log ([dtdate],[sthread],[slevel],[slogger],[smessage],[sexception]) 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="50" /> 

                        <layout type="log4net.layout.patternlayout"> 

                            <conversionpattern value="%t" /> 

                        </layout>                   

                    </parameter> 

                    <parameter> 

                        <parametername value="@log_level" /> 

                        <dbtype value="string" /> 

                        <size value="200" /> 

                        <layout type="log4net.layout.patternlayout"> 

                            <conversionpattern value="%p" /> 

                        </layout> 

                    </parameter> 

                    <parameter> 

                        <parametername value="@logger" /> 

                        <dbtype value="string" /> 

                        <size value="200" /> 

                        <layout type="log4net.layout.patternlayout"> 

                            <conversionpattern value="%logger" /> 

                        </layout> 

                    </parameter> 

                    <parameter> 

                        <parametername value="@message" /> 

                        <dbtype value="string" /> 

                        <size value="2000" /> 

                        <layout type="log4net.layout.patternlayout"> 

                            <conversionpattern value="%m" /> 

                        </layout> 

                    </parameter> 

                    <parameter> 

                        <parametername value="@exception" /> 

                        <dbtype value="string" /> 

                        <size value="2000" /> 

                        <layout type="log4net.layout.exceptionlayout" /> 

                    </parameter> 

                </appender> 

        

        <!-- setup the root category, add the appenders and set the default level --> 

        <root> 

            <level value="warn"/> 

            <level value="info"/> 

            <level value="debug"/> 

            <level value="fine"/> 

        </root> 

        <!-- specify the level for some specific categories --> 

        <logger name="inotes"> 

            <level value="warn"/> 

            <level value="info"/> 

            <level value="debug"/> 

            <level value="fine"/> 

            <appender-ref ref="rollinglogfileappender"/> 

            <appender-ref ref="adonetappender" /> 

        </logger> 

    </log4net>

#2


bmwsl,谢谢你的回复,但是并没有解决我的问题。
   因为我在项目中用到了NHibernate 所以不想让log4net在去获取连接,而是我将NH的连接给Log4net.
请问,怎么做???
  我的想法是可能要自己写一个appender继承log4net.appender.adonetappender,现在的问题就是我得到的ILog对象不知道在哪里地方跟appender有联系(或说不知道ILog通过什么得到他的Appender对象)???

#3


本人用的Log4net 1.2.10版本的,对AdoAppender的初始化数据库连接的方法进行了封装,不能重写(在1.几的版本中是可以的,不知道为什么要封死),调用该方法的地方有两处,虽然可以重写,但是很麻烦,貌似不可能。另外一种方法就是更改AdoAppender源码,在打包,但是,NHibernate报异常,无奈。

   只有放弃了。如果各位有已经实现的可发上源码,或发邮件到 
ainidehsj@163.com || ainidehsj@qq.com

#4


<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="false">

  <appender name="errors" type="log4net.Appender.RollingFileAppender, log4net">
    <file value="Logs/log.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyy-MM-dd" />
    <layout type="log4net.Layout.PatternLayout, log4net">
      <conversionPattern value="%date [%thread] %-5level %logger - %exception%newline" />
    </layout>
  </appender>
  <root>
    <level value="ERROR" />
    <appender-ref ref="errors" />
  </root>

<!--
  <appender name="nhSql" type="log4net.Appender.FileAppender, log4net">
<file value="Logs/nhSql.txt" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout, log4net">
      <conversionPattern value="%date %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <logger name="NHibernate.SQL" additivity="false">
    <level value="ALL" />
    <appender-ref ref="nhSql" />
  </logger>
-->

</log4net>

#5


还需要配置下:
   void Application_Start(object sender, EventArgs e) 
    {
        //配置log4net。
        string VirtualPath = System.Configuration.ConfigurationManager.AppSettings["log4net.config"];
        string PhysicalPath = System.Web.HttpContext.Current.Request.MapPath(VirtualPath);
        log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(PhysicalPath));
    }

#6


nhibernate.config:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
<session-factory name="PermissionBase.Core">

    <!-- DB2 9 -->
    <!--
<property name="connection.connection_string">driver={IBM DB2 ODBC DRIVER};Database=PERMBASE;hostname=localhost;port=50000;protocol=TCPIP;uid=Administrator;pwd=myPassword</property>
<property name="connection.driver_class">NHibernate.Driver.OdbcDriver</property>
<property name="dialect">NHibernate.Dialect.DB2Dialect</property>
-->
    
    <!-- MySQL 5 -->
<!--
<property name="connection.connection_string">Server=localhost;Database=PermissionBase;User Id=PB_DB_USER;Password=1234567890</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
-->
    
<!-- Oracle 10g -->
    <!--
<property name="connection.connection_string">Data Source=CAIKELUN;User Id=PB_DB_USER;Password=1234567890;Integrated Security=no</property>
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="dialect">NHibernate.Dialect.Oracle9Dialect</property>
-->
    
<!-- SQL Server 2000/2005 -->
<property name="connection.connection_string">Server=067-LIUCHAO\SQL2005;Database=PermissionBase;User Id=sa;Password=lc!44216791</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>

<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="show_sql">true</property>
<property name="use_outer_join">true</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
<property name="cache.use_query_cache">true</property>
<property name="relativeExpiration">120</property>
<mapping assembly="PermissionBase.Core" />

</session-factory>
</hibernate-configuration>

#1


<log4net> 

… 

<appender name="adonetappender" type="log4net.appender.adonetappender"> 

                    <buffersize value="10" /> 

                    <connectiontype value="system.data.sqlclient.sqlconnection, system.data, version=1.0.3300.0, culture=neutral, publickeytoken=b77a5c561934e089" /> 

                    <connectionstring value="server=192.168.1.112;database=domino;uid=sa;pwd=;timeout=300;" /> 

                    <commandtext value="insert into mail_web_log ([dtdate],[sthread],[slevel],[slogger],[smessage],[sexception]) 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="50" /> 

                        <layout type="log4net.layout.patternlayout"> 

                            <conversionpattern value="%t" /> 

                        </layout>                   

                    </parameter> 

                    <parameter> 

                        <parametername value="@log_level" /> 

                        <dbtype value="string" /> 

                        <size value="200" /> 

                        <layout type="log4net.layout.patternlayout"> 

                            <conversionpattern value="%p" /> 

                        </layout> 

                    </parameter> 

                    <parameter> 

                        <parametername value="@logger" /> 

                        <dbtype value="string" /> 

                        <size value="200" /> 

                        <layout type="log4net.layout.patternlayout"> 

                            <conversionpattern value="%logger" /> 

                        </layout> 

                    </parameter> 

                    <parameter> 

                        <parametername value="@message" /> 

                        <dbtype value="string" /> 

                        <size value="2000" /> 

                        <layout type="log4net.layout.patternlayout"> 

                            <conversionpattern value="%m" /> 

                        </layout> 

                    </parameter> 

                    <parameter> 

                        <parametername value="@exception" /> 

                        <dbtype value="string" /> 

                        <size value="2000" /> 

                        <layout type="log4net.layout.exceptionlayout" /> 

                    </parameter> 

                </appender> 

        

        <!-- setup the root category, add the appenders and set the default level --> 

        <root> 

            <level value="warn"/> 

            <level value="info"/> 

            <level value="debug"/> 

            <level value="fine"/> 

        </root> 

        <!-- specify the level for some specific categories --> 

        <logger name="inotes"> 

            <level value="warn"/> 

            <level value="info"/> 

            <level value="debug"/> 

            <level value="fine"/> 

            <appender-ref ref="rollinglogfileappender"/> 

            <appender-ref ref="adonetappender" /> 

        </logger> 

    </log4net>

#2


bmwsl,谢谢你的回复,但是并没有解决我的问题。
   因为我在项目中用到了NHibernate 所以不想让log4net在去获取连接,而是我将NH的连接给Log4net.
请问,怎么做???
  我的想法是可能要自己写一个appender继承log4net.appender.adonetappender,现在的问题就是我得到的ILog对象不知道在哪里地方跟appender有联系(或说不知道ILog通过什么得到他的Appender对象)???

#3


本人用的Log4net 1.2.10版本的,对AdoAppender的初始化数据库连接的方法进行了封装,不能重写(在1.几的版本中是可以的,不知道为什么要封死),调用该方法的地方有两处,虽然可以重写,但是很麻烦,貌似不可能。另外一种方法就是更改AdoAppender源码,在打包,但是,NHibernate报异常,无奈。

   只有放弃了。如果各位有已经实现的可发上源码,或发邮件到 
ainidehsj@163.com || ainidehsj@qq.com

#4


<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="false">

  <appender name="errors" type="log4net.Appender.RollingFileAppender, log4net">
    <file value="Logs/log.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyy-MM-dd" />
    <layout type="log4net.Layout.PatternLayout, log4net">
      <conversionPattern value="%date [%thread] %-5level %logger - %exception%newline" />
    </layout>
  </appender>
  <root>
    <level value="ERROR" />
    <appender-ref ref="errors" />
  </root>

<!--
  <appender name="nhSql" type="log4net.Appender.FileAppender, log4net">
<file value="Logs/nhSql.txt" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout, log4net">
      <conversionPattern value="%date %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <logger name="NHibernate.SQL" additivity="false">
    <level value="ALL" />
    <appender-ref ref="nhSql" />
  </logger>
-->

</log4net>

#5


还需要配置下:
   void Application_Start(object sender, EventArgs e) 
    {
        //配置log4net。
        string VirtualPath = System.Configuration.ConfigurationManager.AppSettings["log4net.config"];
        string PhysicalPath = System.Web.HttpContext.Current.Request.MapPath(VirtualPath);
        log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(PhysicalPath));
    }

#6


nhibernate.config:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
<session-factory name="PermissionBase.Core">

    <!-- DB2 9 -->
    <!--
<property name="connection.connection_string">driver={IBM DB2 ODBC DRIVER};Database=PERMBASE;hostname=localhost;port=50000;protocol=TCPIP;uid=Administrator;pwd=myPassword</property>
<property name="connection.driver_class">NHibernate.Driver.OdbcDriver</property>
<property name="dialect">NHibernate.Dialect.DB2Dialect</property>
-->
    
    <!-- MySQL 5 -->
<!--
<property name="connection.connection_string">Server=localhost;Database=PermissionBase;User Id=PB_DB_USER;Password=1234567890</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
-->
    
<!-- Oracle 10g -->
    <!--
<property name="connection.connection_string">Data Source=CAIKELUN;User Id=PB_DB_USER;Password=1234567890;Integrated Security=no</property>
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="dialect">NHibernate.Dialect.Oracle9Dialect</property>
-->
    
<!-- SQL Server 2000/2005 -->
<property name="connection.connection_string">Server=067-LIUCHAO\SQL2005;Database=PermissionBase;User Id=sa;Password=lc!44216791</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>

<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="show_sql">true</property>
<property name="use_outer_join">true</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
<property name="cache.use_query_cache">true</property>
<property name="relativeExpiration">120</property>
<mapping assembly="PermissionBase.Core" />

</session-factory>
</hibernate-configuration>