开发者

Is it possible to use Log4Net to write to log file within a CLR Stored Procedure?

Issue: Unable to debug (write) to a Log File using Log4net inside of a CLR Stored Procedure. Possibly a problem with the way I'm building the CLR project? I'm only importing the DLL's into sql server (create assembly....). Do I need to import the App.Config as well?

DLL Name: CLRTest.dll

Source Code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server; 
using log4net;
using log4net.Config;

public class MyClass
{
    private readonly static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private static readonly string sContextConn = "Context Connection=true";

    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void Select1()
    {
        XmlConfigurator.Configure();
        log.Debug("Begin Select1()...");
        using (SqlConnection connection = new SqlConnection(sContextConn))
        {
            connection.Open();
            SqlCommand command = new SqlCommand("select 1", connection);
            SqlDataReader r = command.ExecuteReader();
            SqlContext.Pipe.Send(r);
        }

        log.Debug("End Select1()...");
    }
}//end MyClass

Log4Net XML Config (App.Config):

    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>

    <log4net>
   开发者_StackOverflow中文版 <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="C:\log\clrsql.log" />
      <param name="AppendToFile" value="true" />
          <datePattern value="yyyyMMdd-HHmm" />
      <param name="rollingStyle" value="Size" />
      <param name="maxSizeRollBackups" value="50" />
      <param name="maximumFileSize" value="25MB" />
      <param name="staticLogFileName" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d %-5p [t-%t] [%c.%M(%L)] %m%n" />
      </layout>
    </appender>


    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d %-5p [%c.%M(%L)] %m%n" />
      </layout>
    </appender>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingFileAppender" />
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>

Permissions on SQL Server:

C:\log\ -- NETWORK SERVICE and MyDomain\sqlserveraccount have Full Control on the "log" folder.

SQL Scripts:

drop procedure clr_Select1
go
drop assembly CLRTest
go
create ASSEMBLY CLRTest FROM 'C:\Share\ClrSql\TEST\CLRTest.dll' WITH PERMISSION_SET = unsafe
go
CREATE PROCEDURE clr_Select1 
    AS EXTERNAL NAME CLRTest.MyClass.Select1
go
exec clr_Select1 

SQL Output:

(No column name)

1


The log4net configurator call that you're making actually tries to find the AppDomain.CurrentDomain.SetupInformation.ConfigurationFile. I don't know what the AppDomain is for a stored procedure.

You might want to try a standalone configuration file and use the overload of that configurator.

log4net.Config.XmlConfigurator.Configure(new FileInfo("config.log4net"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜