Can I use Log4Net to write to a log file in a CLR stored procedure?

Problem. It is not possible to debug (write) to a log file using Log4net inside the CLR stored procedure. Perhaps the problem is with how I build the CLR project? I only import the DLL into the sql server (create an assembly ...). Do I need to import App.Config as well?

DLL Name: CLRTest.dll

Source:

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>
    <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 access to 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

+3
source share
1 answer

log4net, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile. , AppDomain .

.

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

All Articles