How to configure multiple loggers in dll - log4net

I'm new to log4net and I'm not quite sure how to set up my app.config correctly when there are multiple loggers in a dll. Well I do but I'm wondering if there is an easier way when there are 10-20 different loggers. It may also be that I just don't understand this.
Ok so let's say the dll uses the following loggers (it's actually about 20 different statements like this):
private static readonly ILog log = LogManager.GetLogger(typeof(XmlConfiguration));
private static readonly ILog log = LogManager.GetLogger(typeof(ClassValidator));
Does this mean I need to do something like this in my app.config (I have omitted appenders for brevity)?
<logger name="XmlConfiguration" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="FileLog"/>
</logger>
<logger name="ClassValidator" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="FileLog"/>
</logger>
Can I configure it somehow such that each logger exposed by a particular dll or partial namespace (loggers in dll are under several different namespaces but have a common root namespace) all go to the same logging source (file, console etc)?

Yes, you are correct. You can set different login levels for different dlls by configuring multiple loggers :
Here is an example i have used successfully :
<!-- ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF -->
<root>
<level value="Error"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
<logger name="NHibernate">
<level value="ERROR" />
</logger>

Related

Log4Net on cloud solution?

I would like to utilize Log4Net on a Dynamics 365 solution (plugins, etc.)
Is this possible somehow - I cannot just deploy a config file I guess, but are there a feasible way to do it anyhow?
The dream of mine is to unify logging, to place logging in one place, no matter if it is plug-ins, integrations or other functionality...
Besides using a configuration file, Log4net can accept its configuration from other sources using eg. a Stream or XmlElement.
To do so, you'll have to use the Log4net api via XmlConfigurator.Configure.
It is important not to apply the XmlConfiguratorAttribute; it's either one or the other.
The example below shows how to apply a configuration via an XmlElement.
The content of this XmlElement can be retrieved from anywhere; hardcoded, an embedded resource file, a record from a dabase, etc.
In the xml configuration, you declare any loggers that can be used within your Dynamics 365 environment.
string xml = #"
<log4net>
<appender name='consoleAppender' type='log4net.Appender.ConsoleAppender' >
<layout type='log4net.Layout.PatternLayout'>
<conversionPattern value='%date | %logger | %level | %message | %exception%n' />
</layout>
</appender>
<root>
<level value='ALL' />
<appender-ref ref='consoleAppender' />
</root>
</log4net>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlElement config = doc.DocumentElement;
ILoggerRepository repository = log4net.LogManager.GetRepository(Assembly.GetCallingAssembly());
XmlConfigurator.Configure(repository, config);
ILog logger = LogManager.GetLogger("somelog");
logger.Debug("foobar");

Sitecore - Filter out Sitecore Errors

We are hosting sitecore 8.1 in azure web apps. We log the errors on to App Dynamic where the application ticket will auto-created for each error.
The problem is that 90% of errors are raised by builtin sitecore piepelines e.g. GeoIP service not setup, session end not setup (due to MongoDB) etc.
Is it possible to identify and filter out the errors raised by built in pipelines and events in Sitecore?
Thanks.
Sitecore uses Log4Net as a framework for its logging. I haven't done it myself but I'd bet you can use standard L4N techniques to filter messages. It's probably just a config tweak or two.
You might prefer instead to spend the same time and research effort correcting the issues in Sitecore? Most services can be disabled if you aren't using them (and if they are raising errors then they aren't working anyway, right). Remove items from pipelines, disable tracking.
This way not only will you see fewer errors in your logs, your site will go faster as well!
If you only want the log files to log errors in the system, you need to set your logging level to ERROR in the log4net setup.
<log4net>
<!-- The appenders will go here -->
<root>
<priority value="ERROR" />
<appender-ref ref="LogFileAppender" />
</root>
<logger name="Sitecore.Diagnostics.WebDAV" additivity="false">
<level value="ERROR" />
<appender-ref ref="WebDAVLogFileAppender" />
</logger>
<logger name="Sitecore.Diagnostics.Search" additivity="false">
<level value="ERROR" />
<appender-ref ref="SearchLogFileAppender" />
</logger>
<logger name="Sitecore.Diagnostics.Crawling" additivity="false">
<level value="ERROR" />
<appender-ref ref="CrawlingLogFileAppender" />
</logger>
<logger name="Sitecore.Diagnostics.Publishing" additivity="false">
<level value="ERROR" />
<appender-ref ref="PublishingLogFileAppender" />
</logger>
<logger name="Sitecore.FXM.Diagnostics" additivity="false">
<level value="ERROR" />
<appender-ref ref="FxmLogFileAppender" />
</logger>
</log4net>
If you want to disable a particular log file, set the level value to NONE.
Bare in mind tho, setting the level to ERROR will stop all info and warning entries from being logged, so you may miss important entries for debugging issues.
You can just use the default Log4net and write to the same log file that sitecore uses, you dont even need to modify the webconfig to do this. It will be something like this:
Sitecore.Diagnostics.Log.Error(
Once you type this you'll see the different options you have to construct the log error. For identifying the type and where it came from you can check the exception type and stack and go from there, in this case you should create a global exception handling class and pull all the exception in there and run them through your identification/categorization methods!
Make sure you reference this 2:
using log4net;
using Sitecore.Diagnostics;

spring integration wire-tap and logging-channel-adapter logging issue

I am trying to add wire-tap to intercept the channel but it is not using my logback configuration but instead it uses org.apache.commons.logging.impl.Jdk14Logger.
How do tell LoggingHandler.messageLogger to use lmy logback configuration?
<int:channel id="testChannel">
<int:interceptors>
<int:wire-tap channel="loggerChannel"/>
</int:interceptors>
</int:channel>
<int:logging-channel-adapter id="loggerChannel" level="DEBUG"/>
My logback has following configurayion defined
<logger name="org.springframework" level="DEBUG"/>
<logger name="org.springframework.integration" level="DEBUG"/>
<logger name="org.springframework.integration.handler.LoggingHandler" level="DEBUG"/>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ROLLING"/>
</root>
For historical reasons, Spring uses commons-logging internally; you can wire in logback using slf4j.
See the note in the Spring Reference.
Here's another howto.
Essentially you need to exclude commons-logging from spring deps and add jcl-over-slf4j.

How do I prevent logging of 404 Not Found exceptions?

I'm using ServiceStack and ServiceStack.Logging.Log4Net. With the minimum config in my AppHost file:
log4net.Config.XmlConfigurator.Configure();
LogManager.LogFactory = new Log4NetFactory(true);
Request handler not found exceptions (i.e. no route was matched) are magically picked up and logged. I don't want to log these exceptions, but I can't figure out how to exclude them.
I've tried setting a custom exception handler:
ExceptionHandler = _appExceptionLogger.OnAppHostException;
but it doesn't appear to get hit for these exceptions.
So I searched in the ServiceStack source code for the string that was being logged and found that it was being logged by a NotFoundHttpHandler class
This means that I can override the logging behaviour by adding a custom logger to my log4net config:
ServiceStack v3:
<logger name="ServiceStack.WebHost.Endpoints.Support.NotFoundHttpHandler">
<level value="OFF" />
</logger>
ServiceStack v4:
<logger name="ServiceStack.Host.Handlers.NotFoundHttpHandler">
<level value="OFF" />
</logger>

mule log files into DB

In mule CE version 3.3.0, I have a mule project, and the URL for calling it is http://localhost:8086/mule?msg=Hello-World!!!.
Every time that I call it, in a log file in mule server that it has as a default some lines added as a log.
Now I want to change type of logs in mule? I want to instead the file that mule create it for logs, as a default mule create a table into Database and save important info into it.
Actually I want to have a table log for my projects, and in this table I want to have customers' information such as IP and ...
Is it possible?
How can I do it?
You can use Log4j database appender to insert Mule ESB logs into database. Below code snippet is used to do the same.
<appender name="DB" class="org.apache.log4j.jdbc.JDBCAppender">
<param name="url" value="jdbc:mysql://localhost/DBNAME"/>
<param name="driver" value="com.mysql.jdbc.Driver"/>
<param name="user" value="user_id"/>
<param name="password" value="password"/>
<param name="sql" value="INSERT INTO LOGS VALUES('%level','%message','%X{muleMessage}','%X{payload}')"/>
<layout class="org.apache.log4j.PatternLayout">
</layout>
</appender>
<logger name="log4j.rootLogger" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="DB"/>
</logger>

Resources