问题
I have been learning log4net and wish to use the remoting appender to log messages onto a server sometime in the future. To do this, I first tried creating a local .Net remoting server and appending to it. It seems to me that the server has been created but I cannot receive these messages. (To check this, I try accessing the server by entering localhost:portnumber in my browser, before and after running my program. It fails before and accepts the connection later. Any better way to debug this?) Anyway, here is the code. I would appreciate any help. PS: I can see the File & Console appenders work.
Client code App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.onfig.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="Console" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="DEBUG"/>
<foreColor value="Red, HighIntensity"/>
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%class %date [%level] - %message%newline"/>
</layout>
</appender>
<appender name="File" type="log4net.Appender.FileAppender">
<file value="logfile.txt"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%class %date [%level] - %message%newline"/>
</layout>
</appender>
<appender name="RemotingAppender" type="log4net.Appender.RemotingAppender">
<sink value="tcp://localhost:8086/RemoteLogger"/>
<lossy value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%class %date [%level] - %message%newline"/>
</layout>
<bufferSize value="1"/>
<onlyFixPartialEventData value="true"/>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="Console"/>
<appender-ref ref="RemotingAppender"/>
<appender-ref ref="File"/>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Server code Appconfig
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="RemoteLogger">
<channels>
<channel name="logging sink" ref="tcp server" port="8086"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
Server Code
namespace RemoteAPP
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Listening");
var _sink = new RemoteSink();
_sink.EventsReached += (s,a)=> AddLog(a.LoggingEvents);
RemotingConfiguration.Configure("RemoteAPP.exe.config", false);
//RemotingConfiguration.RegisterWellKnownServiceType(new WellKnownServiceTypeEntry(typeof(RemoteSink), "RemoteLogger", WellKnownObjectMode.SingleCall));
RemotingServices.Marshal(_sink, "RemoteLogger");
Console.ReadLine();
}
private static void AddLog(IEnumerable<LoggingEvent> enumerable)
{
var Logevents = enumerable.ToList();
foreach(var logevent in Logevents)
{
Console.WriteLine(logevent);
}
}
}
public class RemoteSink:MarshalByRefObject,RemotingAppender.IRemoteLoggingSink
{
public class LoggingArgs:EventArgs
{
public IEnumerable<LoggingEvent> LoggingEvents;
}
public EventHandler<LoggingArgs> EventsReached;
void RemotingAppender.IRemoteLoggingSink.LogEvents(LoggingEvent[] events)
{
var ev = EventsReached;
if(ev==null)
{
ev.Invoke(this, new LoggingArgs{LoggingEvents = events});
}
}
}
}
Client Code
class Program
{
static void Main(string[] args)
{
Thread.Sleep(10000);
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
for (int i = 0; i < 1000;i++ )
{
log.Info("Hello world");
log.Debug("This is Debug");
log.Warn("This is Warn");
}
回答1:
Well, I found the answer. The code above is correct. Turns out there was some incompatibility between the client and server side of Log4net. Although I installed both from the same source (Nuget), something changed between the two references. After setting log4net's internal debugger property to true, I stumbled across this log message.
log4net:ERROR [RemotingAppender] ErrorCode: GenericFailure. Failed in SendBufferCallback
Googling it led me to here and this is where I suspected something was amiss. Long story short, removed all references to log4net in RemoteAPP and reinstalled via Nuget.
It works now. If someone knows how this occurred, please continue the discussion.
来源:https://stackoverflow.com/questions/32030500/remotingappender-problems-conn-created-but-nothing-appended