locked
LightSwitch and log4net RRS feed

  • Question

  • I have integrated the log4net framework in lightswitch and got it working while debugging but as soon as I deploy it to IIS the log framework does not work any longer. I have put the log4net configuration inside the web.config file lying under the ServerGenerated project, and put the trigger to read in the configuration in the AssemblyInfo.cs file in the same project

    assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)]

    My feeling is that it´s just this configuration trigger which is not working when deployed to IIS. Anyone that knows a better place to put the trigger in LightSwich? All help are appreciated.

    Regards

    Tommy

     


    Tommy Norman
    Wednesday, October 26, 2011 12:40 PM

All replies

  • Hi, Tommy

    If your log4net appender "AppendToFile" then you must add "write" permission on the app-folder for your users (probably IIS_IUSRS).

    (i use custom "AdoNetAppender", which append to db, but just now switch to "AppendToFile" and haven't see any problem except this one) 

    • Edited by LR__ Wednesday, October 26, 2011 6:50 PM
    • Proposed as answer by Latish Tuesday, November 1, 2011 9:31 PM
    • Unproposed as answer by Latish Tuesday, November 1, 2011 9:31 PM
    Wednesday, October 26, 2011 6:40 PM
  • I agree, it sounds like a "permissions" problem to me too.

    Yann - LightSwitch Central

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    Thursday, October 27, 2011 1:18 AM
    Moderator
  • Thanks for your replies but I´m only using the AdoNetAppender and SMTPAppender, no file appender at all, so it doesn´t feel lika a permission problem to me.

    How did you guys configure the log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)] trigger? did you put it in the AssemblyInfo.cs file on the ServerGenerated project as I did or did you do something else?


    Tommy Norman
    Thursday, October 27, 2011 7:15 AM
  • How did you guys configure the log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)] trigger? did you put it in the AssemblyInfo.cs file on the ServerGenerated project as I did or did you do something else?


    Tommy Norman
    I put it in the AssemblyInfo.cs file on the Server project (appender code too), not ServerGenerated... but may it be important? i don't know
    Thursday, October 27, 2011 11:45 AM
  • aha, but where did you put the appender code in the server project? I assume not in the web.config as it´s placed under the ServerGenerated project, did you put it in a log4net.config file placed under server project or how should I intepret it?


    Tommy Norman
    Thursday, October 27, 2011 12:22 PM
  • no, appender code = code of the "public sealed class Log4netAppender : IAppender, IOptionHandler"

    in the web.config(ServerGenerated) the config section only

      <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
      </configSections>
      <log4net>
        <root>
          <appender-ref ref="AdoNetAppender" />
        </root>
        <appender name="AdoNetAppender" type="LightSwitchApplication.Log4netAppender">
          <bufferSize value="1" />
        </appender>
      </log4net>
    
    


    Thursday, October 27, 2011 12:29 PM
  • hmm ok :) could you post your Log4netAppender class as well?
    Tommy Norman
    Thursday, October 27, 2011 1:13 PM
  • using System;
    using System.Data;
    using System.Data.SqlClient;
    using log4net.Appender;
    using log4net.Core;
    
    namespace LightSwitchApplication
    {
        public sealed class Log4netAppender : IAppender, IOptionHandler
        {
            SqlConnection dbConnection;
            string myDbConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
    
            public string Name { get; set; }
            public string ConnectionString { get; set; }
            public int BufferSize { get; set; }
    
            public void ActivateOptions()
            {
                dbConnection = new SqlConnection(myDbConnectionString);
                dbConnection.Open();
            }
            public void Close()
            {
                if (dbConnection != null) {
                    dbConnection.Close();
                }
            }
            public void DoAppend(LoggingEvent loggingEvent)
            {
                try {
                    using (SqlCommand cmd = new SqlCommand("dbo.Log4net_ins", dbConnection)) {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@dt", SqlDbType.DateTime).Value = loggingEvent.TimeStamp;
                        cmd.Parameters.Add("@lvl", SqlDbType.Int).Value = loggingEvent.Level.Value;
                        cmd.Parameters.Add("@className", SqlDbType.VarChar).Value = loggingEvent.LocationInformation.ClassName;
                        cmd.Parameters.Add("@methodName", SqlDbType.VarChar).Value = loggingEvent.LocationInformation.MethodName;
                        cmd.Parameters.Add("@renderedMessage", SqlDbType.VarChar).Value = loggingEvent.RenderedMessage;
                        cmd.ExecuteNonQuery();
                    }
                } catch { }
            }
        }
    }
    
    

    db table structure and t-sql code of the sp dbo.Log4net_ins corresponding to LoggingEvent attributes

    Thursday, October 27, 2011 1:33 PM
  • Ok thanks alot, but doing a custom appender when there already are an AdoNetAppender available in log4net seems a little bit overkill to me. What I can´n understand is why the default AdoNetAppender does not work when it´s deployed to IIS but works fine when running the LS project in debug mode and the fact that the SMTPAppender does not work as well when deployed gives me the feeling that there is a problem to find the log4net configuration section in the deployed version of the web.config file. So is there anyone else out there that have managed to get the log4net to work by using the default configuration of log4net in web.config? 

    <configuration>
      <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
      </configSections>
      <log4net>
        <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
          <bufferSize value="1" />
          <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          <connectionString value="Data Source=GOT-TOMNOR\LOCALSQL;Initial Catalog=ProductCare;Integrated Security=SSPI" />
          <commandText value="INSERT INTO ApplicationLog ([Date],[Thread],[Level],[Logger],[MachineName],[RemoteAddr],[Url],[RefererUrl],[MemberId],[Class],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @machinename, @remote_addr, @url, @referer_url, @memberid, @class, @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="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="@machinename" />
            <dbType value="String" />
            <size value="32" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%property{machinename}" />
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@remote_addr" />
            <dbType value="String" />
            <size value="32" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%property{remote_addr}" />
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@url" />
            <dbType value="String" />
            <size value="255" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%property{url}" />
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@referer_url" />
            <dbType value="String" />
            <size value="255" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%property{referer_url}" />
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@memberid" />
            <dbType value="String" />
            <size value="40" />
            <layout type="log4net.Layout.RawPropertyLayout">
              <key value="memberid" />
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@class" />
            <dbType value="String" />
            <size value="255" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%property{klass}" />
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@message" />
            <dbType value="String" />
            <size value="1024" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%message" />
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@exception" />
            <dbType value="String" />
            <size value="4096" />
            <layout type="log4net.Layout.ExceptionLayout" />
          </parameter>
        </appender>
        <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender,log4net">
          <to value="tommy.norman@edb.com" />
          <from value="productcare@alfalaval.com" />
          <subject value="ProductCare - Error log message" />
          <smtpHost value="localhost" />
          <bufferSize value="20" />
          <lossy value="false" />
          <threshold value="DEBUG" />
          <evaluator type="log4net.Core.LevelEvaluator,log4net">
            <threshold value="ERROR" />
          </evaluator>
          <layout type="log4net.Layout.PatternLayout,log4net">
            <conversionPattern value="%property{log4net:HostName} :: %level :: &#xD;&#xA;        %message %newlineLogger: %logger%newlineThread: %thread%newlineDate: &#xD;&#xA;        %date%newlineNDC: %property{NDC}%newline%newline" />
          </layout>
        </appender>
        <root>
          <level value="Debug" />
          <appender-ref ref="AdoNetAppender" />
          <appender-ref ref="SmtpAppender" />
        </root>
      </log4net>
    </configuration>


    Tommy Norman
    Thursday, October 27, 2011 2:19 PM