TCP / IP日志应用程序 [英] TCP/IP Logging Application

查看:94
本文介绍了TCP / IP日志应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

在这里阅读.Net Remoting

a-simple-remoting-example-in-c / [ ^ ]

dotnetremoting [ ^ ]
Read about .Net Remoting here
a-simple-remoting-example-in-c/[^]
dotnetremoting[^]


这篇关于TCP / IP日志应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆