How do I prevent logging of 404 Not Found exceptions? - servicestack

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>

Related

Azure AppInsight with Log4Net

I have been trying to write Logs(Trace, Information & Exception) in Azure AppInsights using Log4Net instead of default api Telemetry client. When I run the application from VS2013 neither I get any error message nor am seeing logs in Azure portal.
Pleaes help me figure out this issue.
Note: Am using Log4net appender for AppIinsights.
Web.Config
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="aiAppender" />
</root>
<appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
</appender>
MVC Controller
public class HomeController : Controller
{
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public ActionResult Index()
{
//Trace.TraceInformation("Home accessed at : {0}", DateTime.UtcNow);
Log.Info(string.Format("Home accessed at : {0}", DateTime.UtcNow));
return View();
}
}
Regards,
Rajaram.
If you are not seeing any log4net output, i'm presuming you are missing some log4net startup code, like this:
log4net.Config.XmlConfigurator.Configure();
which you might want in your startup class / code somewhere. Without that, log4net doesn't know wo read the configuration that's in web.config.
In addition to the answer from #JohnGardner, you can instead add a line to your AssemblyInfo.cs file as so: -
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
There is more discussion on the two approaches in the following question: -
Configure Log4Net in web application
And in a comment in somewhere in that discussion is a link to the log4net FAQs that touches on the differences in the question "When should I log my first message?": -
https://logging.apache.org/log4net/release/faq.html#first-log
I found both of these to be of further use to me.

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;

log4net config settings

I am into the development of a core dll where I have a class library.I want to use log4net to enable logging for exceptions. I have an app.config file in the class library where i have given the settings for the log4net.However when I test the class library the log4net does'nt create logs until i add the app.config in the calling project inspite of the fact that i had added [assembly: log4net.Config.XmlConfigurator(Watch = true)] in the class libary's assemblyinfo.cs and I am using log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ErrorHandler)) where ErrorHandler is the name of my class library's class where log4net's calling functions are handled.Any ideas on what is going wrong?
Secondly, what I want to acheive is the users of my dll will just pass the location where they want to create logs and whether they want to create logs in event viewer or log files from their app.config? They will not handle any other setting of log4net.
Any suggestions or code snippets for the first issue and the second problem?
Only the "main" app.config is active for a .Net application. Your library config file is simply ignored. Either you transfer your settings to the main config file or you use an external config file for log4net. You configure it then for instance like this (assuming you call it log4net.config):
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Please note that the structure of the config file is a bit different:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="YourAppender" type="..." >
....
</appender>
<root>
<level value="ALL" />
<appender-ref ref="YourAppender" />
</root>
</log4net>
As for your second problem: I am not sure how flexible this has to be. Is it just switching from file appender to event log appender? Depending on your answer you may consider two prepare to configuration files (e.g. file.log4net and eventlog.log4net) and read the configuration as needed (in that case you cannot use the attribute: you call the Configure() method directly) or if your requirements are more complex you might even end up configuring log4net programatically.

JBoss 6.0 M3 and Log4j logging

I'm trying to have application specific logging plus the usual server logs seperately. I have specified 2 in the jboss-logging.xml, one for my app and the other one is usual "server.log" file.
Issue is, both of the log files are getting created and logged at the same time. Can any one help me, what is the change i need to make to log ONLY application specific logs in my logfile?
I fixed this issue. I was defining my application's handler on the section in jboss-logging.xml.Plus i was defining a logger category for my app. The fix was, i removed my application's handler reference from <root-handler> section and hold just the logger category for my app as below:
<logger category="com.X.Y.logger">
<level name="DEBUG"/>
<handlers>
<handler-ref name="myApp"/>
</handlers>
</logger>

How to configure multiple loggers in dll

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>

Resources