RemotingAppender problems. Conn created but nothing appended - log4net

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");
}

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.

Related

How to write log4net config into appsettings.json?

I have implemented log4net into .NET core 2.0, to log into a text file. As log4net have a config file, which is having XML configuration in it. So, I have created a separate file log4net.config to set its configuration and it is working fine. But I want to set its configuration into appsettings.json. Is it possible to write the log4net configuration into appsettings.json.
<appSettings>
<add key="IsLog" value="True" />
<add key="MaxThreads" value="3" />
</appSettings>
<log4net debug="false">
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="./logs/logfile.txt" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="50GB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
</log4net>
this is my XML configuration.
I have found this question in which it is mentioned that log4net don't support Json projects. Is it possible to write its configuration into appsettings.json.
Maybe it is not the right way but, anyhow I managed to use my log4net.config in appSettings.json. I am putting my answer here so it can help someone if they don't want to use an extra config file for there project.
So what I have done is like by converting my XML into JSON and used the JSON as a string on my appSettings.json after that I use the following code to read the string.
appSettings.json
{
"ConnectionStrings": {
"LoggerConfig": "Config string"
}
}
Json to XML Conversion using Newtonsoft.Json
string xmlElement = _configuration["ConnectionStrings:LoggerConfig"];
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(xmlElement);
XmlConfigurator.Configure(logRepository, GenerateStreamFromString(doc.InnerXml));
This code is used to convert the JSON into XML but it will not provide the XML content as Element so, I used it as a stream.
For converting the string into a stream I used the following code.
public static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
and it works fine.
Here I used to convert first my XML to JSON and again JSON to XML to
use it as a log4net config, but if anyone wants then use XML
directly as a string, So it will reduce some code.
If you using Microsoft.Extensions.Logging.Log4Net.AspNetCore nuget package, there is a way keep log4net config in appsettings.json (but honestly not very usable).
You can write into appsettings.json (or appsettings.Environment.json for different environments) rules overriding nodes from log4net config file.
Documentation
Example of setting the logging level from appsettings.json.
appsettings.json:
{
"Log4NetCore": {
"PropertyOverrides": [
{
"XPath": "/log4net/root/level",
"Attributes": {
//"value": "ALL"
//"value": "DEBUG"
//"value": "INFO"
"value": "WARN"
//"value": "ERROR"
//"value": "FATAL"
//"value": "OFF"
}
}
]
}
}
You still needs log4net config file with nodes which you want override from appsettings.json:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="example.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" />
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="DebugAppender" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
Registration in Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Add these lines
var loggingOptions = this.Configuration.GetSection("Log4NetCore")
.Get<Log4NetProviderOptions>();
loggerFactory.AddLog4Net(loggingOptions);
app.UseMvc();
}
}

how to implement log4net in c# and what is need of log4net?

i just want to know that, what is use of log4net in application and how i`m gonna implement in c#.
i am done with some of the research that i am put under it.
public static class Logger
{
private static log4net.ILog Log { get; set; }
static Logger()
{
Log = log4net.LogManager.GetLogger(typeof(Logger));
}
public static void Error(object msg)
{
Log.Error(msg);
}
public static void Error(object msg, Exception ex)
{
Log.Error(msg, ex);
}
public static void Error(Exception ex)
{
Log.Error(ex.Message, ex);
}
public static void Info(object msg)
{
Log.Info(msg);
}
}
}
above is just a method that maintain log in separate file i just want to know in advance.
1.First you have to add a reference for log4net.dll if you havn't done that yet.
2.Then you have to add that statement above your stating class declaration. You can put it right bellow your using statements and outside of your class declaration:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
3.The class that you already have created.
4.Add that config to your configSections tag on your web.config/app.config:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
That will allow you to add tags to configure your log.
5.Add that to your configuration tag on your web.config/app.config:
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender"/>
</root>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="./Log/log.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="'.'yyyy_MM_dd" />
<maxSizeRollBackups value="30" />
<maximumFileSize value="100MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date{yyyy/mm/dd HH:mm:ss,fff} %-5level %logger [%thread] %message %exception%newline"/>
</layout>
</appender>
</log4net>
6.Then you just have to call the methods on your Logger class.
If you want more information i'll sugest you to read the blog of that guy Log4Net Tutorials and Resources

Using log4net to log unhandled errors when using OWIN

Can you point me to some log4net tutorial or samples that logs all unhandled exceptions occurring on my MVC Web Api when using OWIN?
I have a Startup class that configures Castle Windsor and OAuth and I'm not sure how application errors are handled for OWIN enabled applications.
I have tried putting the log4net code in the Application_Error event in global.asax but it never gets executed.
public class WebApiApplication : System.Web.HttpApplication {
protected void Application_Start() {
log4net.Config.XmlConfigurator.Configure();
}
protected void Application_Error(object sender,EventArgs e) {
Exception lastException = Server.GetLastError();
var logger = log4net.LogManager.GetLogger(typeof(WebApiApplication));
logger.Fatal(lastException);
}
}
Any ideas why this is?
EDIT: web.config with log4net configuration:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net debug="true">
<logger name="SiteWorksAPI">
<level value="Debug" />
<appender-ref ref="EventLogAppender" />
</logger>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C:\ProgramData\Site\SiteWorksAPILog.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<immediateFlush value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="============================================================%nLogger = %logger.%nLocation = %location.%nUnique Token = %property{x-fcc-uniquetoken}.%nDate = %date{dd MMM yyyy - HH:mm:ss}.%nMessage = %message%n" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<applicationName value="SiteWorks.API" />
<logname value="SiteWorks API Log" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="Date: %date{dd MMM yyyy - HH:mm:ss} %nMessage: %message %nIdentity: %identity %nUnique Token: %property{x-fcc-uniquetoken} %nChange Time Stamp: %property{x-fcc-changedatetime} %nOriginal Time Stamp: %property{x-fcc-originaltimestamp} %nOriginating System: %property{x-fcc-originatingsystem} %nOriginating User: %property{x-fcc-originatingusername} %nRequest Uri: %property{Request} %nResponse Headers: %property{ResponseHeaders}%nLogger: %logger %nThread: %thread %n%n%exception" />
</layout>
</appender>
</log4net>
You're not seeing any log events because you haven't defined a root logger in your config - the only logger you've defined is called SiteWorksAPI so any loggers you create with any other name - for example, the one named after typeof(WebApiApplication) - will not log anything. If you add a breakpoint on the logging statement you'll see that all log levels are disabled.
The appenders defined in a root logger are inherited by all named appenders (unless the child appenders are defined with additivity = false) - but child appenders set their own log level.
<root>
<level value="Debug" />
<appender-ref ref="EventLogAppender" />
</root>
Edit: there's also another thing to consider - your defined logger only uses the EventLogAppender - note that in order to log to the Event Log, the documentation states EventLogAppender will fail if you try to write using an event source that doesn't exist unless it is running with local administrator privileges but the event source needs to be created by an elevated process.
For testing purposes, you might want to add your FileAppender to the logger.

In Log4J, why %C in ConversionPattern prints '?' (question mark) with AsyncAppender?

I have a trouble when using %C in ConversionPattern with AsyncAppender.
My Lo4J configuration is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss,SSS} %C{1} - %m%n" />
</layout>
</appender>
<appender name="async_console" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="1000" />
<appender-ref ref="console" />
</appender>
<root>
<level value="debug" />
<!--
<appender-ref ref="console" />
-->
<appender-ref ref="async_console" />
</root>
</log4j:configuration>
And my test code is:
#Test
public void testAsync() {
DOMConfigurator
.configure("src/test/resources/learningtest/log4j/log4j_test_async.xml");
Logger log = Logger.getLogger(getClass());
log.debug("Hello, world!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
The result of the test code is:
2012/03/15 11:51:22,570 ? - Hello, world!
Without AsynAppender, it works fine:
2012/03/15 11:51:06,002 Log4jTest - Hello, world!
With %c (category), it works fine, too.
What am I missing?
Please let me know.
Thanks in advance :-)
Reference:
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
When using "%C" or "%M", log4J uses Throwable.getStackTrace to get the stackTrace and use this information to get the caller class and method.
The problem is that when using an AsyncAppender, the Throwable is created in another thread and the stackTrace does not contain the caller method.

Is it possible to use Log4Net to write to log file within a CLR Stored Procedure?

Issue: Unable to debug (write) to a Log File using Log4net inside of a CLR Stored Procedure. Possibly a problem with the way I'm building the CLR project? I'm only importing the DLL's into sql server (create assembly....). Do I need to import the App.Config as well?
DLL Name:
CLRTest.dll
Source Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using log4net;
using log4net.Config;
public class MyClass
{
private readonly static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static readonly string sContextConn = "Context Connection=true";
[Microsoft.SqlServer.Server.SqlProcedure]
public static void Select1()
{
XmlConfigurator.Configure();
log.Debug("Begin Select1()...");
using (SqlConnection connection = new SqlConnection(sContextConn))
{
connection.Open();
SqlCommand command = new SqlCommand("select 1", connection);
SqlDataReader r = command.ExecuteReader();
SqlContext.Pipe.Send(r);
}
log.Debug("End Select1()...");
}
}//end MyClass
Log4Net XML Config (App.Config):
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\log\clrsql.log" />
<param name="AppendToFile" value="true" />
<datePattern value="yyyyMMdd-HHmm" />
<param name="rollingStyle" value="Size" />
<param name="maxSizeRollBackups" value="50" />
<param name="maximumFileSize" value="25MB" />
<param name="staticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [t-%t] [%c.%M(%L)] %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %-5p [%c.%M(%L)] %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
Permissions on SQL Server:
C:\log\ --
NETWORK SERVICE and MyDomain\sqlserveraccount have Full Control on the "log" folder.
SQL Scripts:
drop procedure clr_Select1
go
drop assembly CLRTest
go
create ASSEMBLY CLRTest FROM 'C:\Share\ClrSql\TEST\CLRTest.dll' WITH PERMISSION_SET = unsafe
go
CREATE PROCEDURE clr_Select1
AS EXTERNAL NAME CLRTest.MyClass.Select1
go
exec clr_Select1
SQL Output:
(No column name)
1
The log4net configurator call that you're making actually tries to find the AppDomain.CurrentDomain.SetupInformation.ConfigurationFile. I don't know what the AppDomain is for a stored procedure.
You might want to try a standalone configuration file and use the overload of that configurator.
log4net.Config.XmlConfigurator.Configure(new FileInfo("config.log4net"));

Resources