Hi all,

I'm trying to programatically configure an RemoteAppender on the client
side, but I end up in an exception (see below). Apparently the callback
is not set, and looking through the source code for log4net I cannot
find any obvious reason for the callback to not be set.

I have the setup running by configuring the RemoteAppender from a config
file, but I need to set the logging server based on information given at
runtime.
The code I used to try and achieve my goal is inserted below.

Thank you for any help or hints.

/Mikael

===== Exception begin =====
log4net:ERROR [RemotingAppender] ErrorCode: GenericFailure. Failed in
SendBufferCallback
System.Runtime.Remoting.RemotingException: Server encountered an
internal error. For more information, turn off customErrors in the
server's .config file.

Server stack trace:

Exception rethrown at [0]:
   at
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
reqMsg, IMessage retMsg)
   at
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&
msgData, Int32 type)
   at
log4net.Appender.RemotingAppender.IRemoteLoggingSink.LogEvents(LoggingEvent[]
events)
   at log4net.Appender.RemotingAppender.SendBufferCallback(Object state)
===== Exception end =====

===== Server.vb begin =====
'Make log4net look for logging.config
<Assembly: log4net.Config.XmlConfigurator(ConfigFile:="logging.config",
Watch:=True)>

Namespace My
  Partial Friend Class MyApplication
    Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e
As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles
Me.Startup
      'Start the logging service
      StartLogging()
    End Sub

Private Sub StartLogging()
      Trace.WriteLine("Starting logging server...", "Info")

      Try
        'Configure remoting. This loads the TCP channel as specified in
the .config file.
System.Runtime.Remoting.RemotingConfiguration.Configure("logging.config", False)
'Using logging.config as configuration file

        'Publish the remote logging server. This is done using the
log4net plugin.
        log4net.LogManager.GetRepository().PluginMap.Add(New
log4net.Plugin.RemoteLoggingServerPlugin("LoggingSink"))
      Catch ex As Exception
        Trace.WriteLine("Exception: " + ex.Message, "Error")
      End Try
    End Sub
  End Class
===== Server.vb end =====

===== logging.config begin (server) =====
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <appSettings>
    <add key="log4net.Config" value="logging.config" />
    <add key="log4net.Config.Watch" value="True" />
    <!--<add key="log4net.Internal.Debug" value="true"/>-->
  </appSettings>

  <log4net>
    <appender name="FileAppender"
type="log4net.Appender.RollingFileAppender">
      <file value="C:\logs\TunstallLogs.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="20"/>
      <maximumFileSize value="100MB"/>
      <countDirection value="-1"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger
(%property{log4net:HostName}) [%ndc] - %message%newline" />
      </layout>
    </appender>

    <root>
      <level value="VERBOSE" />
      <appender-ref ref="FileAppender" />
    </root>
  </log4net>
  <system.runtime.remoting>
    <application name="Log4netRemotingServer">
      <!-- We need to define the remoting channels on which we will publish
           the remote logging sink. -->
      <channels>
        <channel displayName="Server Channel" ref="tcp server"
port="8085"/>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>
===== logging.config end (server) =====


===== Client.vb begin =====
Namespace My
  Partial Friend Class MyApplication
    Public Shared Tlog As log4net.ILog

    Private Sub ConfigureLog()
      Dim root As log4net.Repository.Hierarchy.Logger
      root = CType(LogManager.GetRepository(), Hierarchy).Root
      root.AddAppender(FileAppender())
      root.AddAppender(RemoteAppender())
      root.Repository.Configured = True
    End Sub

    Private Function FileAppender() As IAppender
      Dim localFileAppender As log4net.Appender.FileAppender = New
FileAppender()
      localFileAppender.Name = "file"
      localFileAppender.AppendToFile = True
      localFileAppender.File = "C:\logs\filelog.txt"
      localFileAppender.Layout = New
log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level
[%2thread] %message (%logger{1}:%line)%n")
      localFileAppender.Threshold = log4net.Core.Level.All
      localFileAppender.ActivateOptions()

      Return localFileAppender
    End Function

    Private Function RemoteAppender() As IAppender
      Dim localRemoteAppender As log4net.Appender.RemotingAppender =
New RemotingAppender()
      localRemoteAppender.BufferSize = 95
      localRemoteAppender.Layout = New
log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level
[%2thread] %message (%logger{1}:%line)%n")
      localRemoteAppender.Fix = log4net.Core.FixFlags.Domain
      localRemoteAppender.Lossy = False
      localRemoteAppender.Name = "Remote"
      localRemoteAppender.Threshold = log4net.Core.Level.All
      localRemoteAppender.OnlyFixPartialEventData = True
      'localRemoteAppender.Sink = "tcp://" +
My.Settings.CT_PrimaryServerIP + ":8085/LoggingSink"
      localRemoteAppender.Sink = "tcp://192.168.10.11:8085/LoggingSink"
      localRemoteAppender.ActivateOptions()

      Return localRemoteAppender
    End Function

    Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e
As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles
Me.Startup

      ConfigureLog()
      Tlog =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString
+ " [Default instance]")

      Tlog.Info("Some info...")

      'Some more work done here.....
    End Sub

===== Client.vb end =====

Reply via email to