Exceptions not being logged to console. - spring-mvc

I am relatively new to Spring MVC framework and it's logging concepts. When I run an application and an exception occurs(For Ex: NullPointerException), the stack trace is being displayed on the browser itself. the console just says there is an exception.
My problem is that most of the stack trace is not visible on the browser because the layout does not have enough width. Is there a way to make the stack trace print in the console? Thanks.
Here is my log4j.xml in case:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<!-- Application Loggers -->
<logger name="com.npc">
<level value="info" />
</logger>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="info" />
</logger>
<logger name="org.hibernate">
<level value="info" />
</logger>
<logger name="org.springframework.beans">
<level value="info" />
</logger>
<logger name="org.springframework.context">
<level value="info" />
</logger>
<logger name="org.springframework.web">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="info" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
</log4j:configuration>

Try specify
<param name="ConversionPattern" value="%-5p: %c - %m%n%ex" />

Related

log4net - multiple instances - one in global.asax and another in .net app itself

I have modified global.asax to user log4net to log all requests and responses to an underlying webservice.
The webservice also uses log4net, and the two instances are not working independently of each other.
Both instances must use their own config file, as they log to different locations.
Is it possible to have two completely separate instances of log4net running, one in global.asax.cs?
You can configure this required setup within a single configuration file.
Just use dedicated logger instances, each having its own appender instance(s), which output to a separate location.
In your webservice, use a logger named eg. WebServiceLogger.
log4net.ILog logger = LogManager.GetLogger("WebServiceLogger");`
Configure this logger to use an appender named eg. WebServiceAppender
and give it its own output file eg. c:\logs\webservice.log.
The setting additivity=false ensures that this logger will not use any inherited default/root appenders.
<logger name="WebServiceLogger" additivity="false">
<level value="ALL" />
<appender-ref ref="WebServiceAppender" />
</logger>
<appender name="WebServiceAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\logs\webservice.log" />
<!-- other settings go here -->
</appender>
Configure Global.asax in a similar way.
log4net.ILog logger = LogManager.GetLogger("GlobalAsaxLogger");
<logger name="GlobalAsaxLogger" additivity="false">
<level value="ALL" />
<appender-ref ref="GlobalAsaxAppender" />
</logger>
<appender name="GlobalAsaxAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\logs\globalasax.log" />
<!-- other settings go here -->
</appender>
Any other declared loggers will use the root logger, here writing to the appender with name GeneralAppender.
Full configuration:
<log4net>
<logger name="WebServiceLogger" additivity="false">
<level value="ALL" />
<appender-ref ref="WebServiceAppender" />
</logger>
<appender name="WebServiceAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\logs\webservice.log" />
<!-- other settings go here -->
</appender>
<logger name="GlobalAsaxLogger" additivity="false">
<level value="ALL" />
<appender-ref ref="GlobalAsaxAppender" />
</logger>
<appender name="GlobalAsaxAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\logs\webservice.log" />
<!-- other settings go here -->
</appender>
<appender name="GeneralAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\logs\general.log" />
<!-- other settings go here -->
</appender>
<root>
<level value="ALL" />
<appender-ref ref="GeneralAppender" />
</root>
</log4net>

Flyway logging with log4j?

hy guys,
is there a best practice of how to get flyway output into log4j logs?
I'm currently running with following log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="CA" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-p - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="INFO"/>
<param name="levelMax" value="ERROR"/>
</filter>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false"/>
<param name="file" value="log.out"/>
<param name="immediateFlush" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<logger name="com.googlecode.flyway.core.migration" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="fileAppender"/>
</logger>
<root>
<priority value="all"/>
<appender-ref ref="CA"/>
<appender-ref ref="fileAppender"/>
</root>
</log4j:configuration>
I have already searched for an answer online but could not figure it out.
Flyway will automatically use Log4J if it is available on the classpath.

Unrecognized configuration section log4net. web.config website

I use log4net to log the errors in my web application and it works fine. However if I place the same code in website I get error "Unrecognized configuration section log4net"
here is my web.config section
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" requirePermission="false"/>
<root>
<level value="RELEASE" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="D:\ESSReport\Logs\ESSlog.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="4MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%newline%-5p%d{yyyy-MM-dd hh:mm:ss} [%thread] [%logger] [%line] %newline - %message" />
</layout>
</appender>
I have added dll to my website
You are probably missing configuration section registration.
Here is sample code how you can register custom section:
<configuration>
<configSections>
<sectionGroup name="LoggerConfiguration">
<section name="GPWFLogger" type="GP.Solutions.WF.Entities.LoggerConfiguration,GPWFLogger" allowDefinition="Everywhere" allowLocation="true"/>
</sectionGroup>
</configSections>
<LoggerConfiguration>
<GPWFLogger
ConnectionStringName="ASPNETDB"
LogLevel="Full"
LogPrimaryTarget="SqlServer"
LogFilePath="GPWFwebClient.log" />
</LoggerConfiguration>
Take notice that LoggerConfiguration is registred inside sectionGroup.
You can use this principle in your case.

log4net won't write to the visual web developer console

this is my config for log4net but it wont write to the console
see appender: ConsoleAppender
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="C:\Users\name\Documents\visual studio 2010\Projects\logs\logfile.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C %m%n%newline" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C %m%n%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>
Why doesn't it write to the console?
Edit 1
This is how I initialize it:
log4net.Config.XmlConfigurator.Configure(new FileInfo("C:\\Users\\name\\Documents\\Visual Studio 2010\\Projects\\ExecutionEngine\\Libraries\\App.Config"));
Thanks
You can use the trace appender for this purpose. For instance like this:
<appender name="TraceAppender"
type="log4net.Appender.TraceAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-6level - %message%newline" />
</layout>
</appender>

Log4Net: How to specify different level to separate appenders for ASP.NET

For Log4Net used in ASP.NET,
how do I specify specify different levels to separate appenders in web.config?
Thanks.
Pure guesswork at play here.
Looked at the documentation & thought, this could be the way to do it.
Note: DEBUG level messages are logged to console & WARN level messages are logged to eventlog.
I hope it works :)
<log4net>
<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" type="log4net.Appender.ConsoleAppender">
<!-- A1 uses PatternLayout -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<logger name="DebugLogger">
<level value="DEBUG" />
<appender-ref ref="A1" />
</logger>
<logger name="WarnLogger">
<level value="WARN" />
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
This works for SmtpAppender at least. Guess it will work for other appenders too, but I haven't tested it
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>

Resources