Click here to Skip to main content
15,887,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
Hello

I provided a C# application, which to log messages over a TCP/IP connection to a CONSOLE (a TXT file would also go) to transfer and indicate is. Unfortunately this does not function and I didn't know which changes is necessarily thereby the messages over the TCP/IP connection can be transferred. Logging the messages in a local TXT file functions.I have the following Config (app.config) in my monitor application:

<?xml version="1.0"?>
<configuration>
  <system.runtime.remoting>
    <application>
      <channels>
        <channel ref="tcp" port="8086"/>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

I have in my monitor application Program.cs and RemoteSink.cs the following
source code. Here is the file Program.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;

namespace Monitor
{
    class Program
    {
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("Monitor.exe.config", false);
            RemotingConfiguration.RegisterWellKnownServiceType(new WellKnownServiceTypeEntry(typeof(RemoteSink), "LoggingSink", WellKnownObjectMode.SingleCall));

            Console.WriteLine("Monitor started");
            Console.WriteLine("Press <ENTER> to kill.");
            Console.WriteLine();
            Console.ReadLine();
        }
    }
}

And here is the file RemoteSink.cs

using System;
using System.Collections.Generic;
using System.Text;
using log4net.Appender;
using log4net.Core;

namespace Monitor
{
    public class RemoteSink : MarshalByRefObject, RemotingAppender.IRemoteLoggingSink
    {
        public void LogEvents(LoggingEvent[] events)
        {
            foreach (var loggingEvent in events)
            {
                // %date [%thread] %-5level %logger - %message %newline
                Console.WriteLine("{0} [{1}] {2} {3} - {4}", loggingEvent.TimeStamp.ToLongTimeString(), loggingEvent.ThreadName, loggingEvent.Level.Name, loggingEvent.LoggerName, loggingEvent.RenderedMessage);
            }
        }
    }
}


And the following config (app.config) in my main-application:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
  </configSections>

  <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <threshold value="DEBUG"/>
      <file value="V:\\Logs\\log.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message %newline" />
      </layout>
    </appender>
    <appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" >
      <bufferSize value="2" />
      <sink value="tcp://localhost:8086/LoggingSink" />
      <lossy value="false" />
      <onlyFixPartialEventData value="true" />
    </appender>
    <appender name="BufferRemotingAppender" type="log4net.Appender.BufferingForwardingAppender" >
      <bufferSize value="2" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="DEBUG"/>
      </evaluator>
      <appender-ref ref="RemotingAppender" />
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="FileAppender" />
      <appender-ref ref="BufferRemotingAppender" />
    </root>
  </log4net>
</configuration>

My main-application is a Windows-Form, and has the following source-code:

using System;
using System.Reflection;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using log4net;
using log4net.Config;

//[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace WinAppWithLogging
{
    public partial class Form1 : Form
    {
        //private static readonly ILog log = LogManager.GetLogger(typeof(Form1));
        private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public Form1()
        {
            InitializeComponent();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
            log.Warn("my TCP info");
        }
    }
}
--

Mfg
Dominique
Posted
Comments
Jibesh 19-Dec-12 6:56am    
I dont see any code that instantiate the class 'RemoteSink' in your button click you are just using the Log4Net lib and not the remoting server instance.

1 solution

Read about .Net Remoting here
a-simple-remoting-example-in-c/[^]
dotnetremoting[^]
 
Share this answer
 
Comments
Dominique Krug 9-Jan-13 5:24am    
thanks for your help but the 'monitor' and the 'RemoteSink' is the host application. The class 'program' has the line: RemotingConfiguration.RegisterWellKnownServiceType(new WellKnownServiceTypeEntry(typeof(RemoteSink), "LoggingSink", WellKnownObjectMode.SingleCall));
in order the 'RemoteSink' admits to make.
The main-application is the client and should run on a different system.
Perhaps i must add or change a line in the xml configuration?

Dominique

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900