log4j2 logs debug to file but empty if I switch to info - selenium

My info.log remains empty even though I'm getting logs written for the debug file...
Any help would be greatly appreciated!
Console logs INFO normally
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="DebugFile" fileName="logs/Debug.log" immediateFlush="false" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="InfoFile" fileName="logs/Info.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" level="info"/>
<AppenderRef ref="DebugFile" level="debug"/>
<AppenderRef ref="InfoFile" level="info"/>
</Root>
</Loggers>

Related

Log4Net doesn't send e-mail

This is configuration
<log4net debug="true">
<appender name="RFileAppender" type="log4net.Appender.RollingFileAppender, log4net">
<File value="logs/file.txt" />
<AppendToFile value="true" />
<DatePattern value="_yyyyMMdd" />
<RollingStyle value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %date ==> %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RFileAppender" />
</root>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="mail" />
<from value="mail" />
<subject value="Service Error Log" />
<smtpHost value="SMTPServer.domain.com" />
<bufferSize value="512" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
</layout>
</appender>
<root>
<level value="Error" />
<appender-ref ref="SmtpAppender" />
</root>
</log4net>
This is the method I use to send the email
private void SendErrorLogByMail(string error)
{
mailLogger.Error(error);
}
I think it is the configuration, maybe something is missing or some wrong data but I don't know what, what did I do wrong? I put two appenders because the info and warning log levels must be written in the file, while the error levels in both the file and the mail
UPDATE
<log4net debug="true">
<appender name="RFileAppender" type="log4net.Appender.RollingFileAppender, log4net">
<File value="log.txt"/>
<AppendToFile value="true"/>
<DatePattern value="_yyyyMMdd"/>
<RollingStyle value="Date"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %date ==> %message%newline"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RFileAppender"/>
</root>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="from" />
<from value="to" />
<subject value="Error Log" />
<bufferSize value="512" />
<smtpHost value="smtp.gmail.com" />
<port value="587" />
<authentication value="Basic" />
<username value="***" />
<password value="***" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
</layout>
</appender>
<root>
<level value="Error" />
<appender-ref ref="SmtpAppender" />
</root>
</log4net>
Now when log4net is configured this error is returned:
log4net: ERROR [SmtpAppender] ErrorCode: GenericFailure. Error occurred while sending e-mail notification.
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client has not been authenticated. Server response: 5.7.0 Must issue a STARTTLS command first. v18sm36645763edx.30 - gsmtp
in System.Net.Mail.MailCommand.CheckResponse (SmtpStatusCode statusCode, String response)
in System.Net.Mail.MailCommand.Send (SmtpConnection conn, Byte [] command, String from)
in System.Net.Mail.SmtpTransport.SendMail (MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException & exception)
in System.Net.Mail.SmtpClient.Send (MailMessage message)
in log4net.Appender.SmtpAppender.SendEmail (String messageBody)
in log4net.Appender.SmtpAppender.SendBuffer (LoggingEvent [] events)
Look. I'm just seeing your configuration and did you try to change tag value for another host, like Gmail?
I don't have experience with log4net but sometimes when something is wrong to send an email, probably can be the SMTP host, port.
Make senses ?

How to route log messages based on category using log4j2.xml

I am after routing log messages based on category using log4j2.xml.
I found below link but not sure how to achieve it
https://docs.mulesoft.com/mule-user-guide/v/3.8/logger-component-reference#configuring-custom-logging-settings
Can someone please provide an example config for log4j2.xml.
Below is config from my log4j2.xml
<Appenders>
<RollingFile name="file"
fileName="${sys:mule.home}${sys:file.separator}logs
${sys:file.separator}splunktest.log"
filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}splunktest-%i.log">
<PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
<SizeBasedTriggeringPolicy size="10 MB" />
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="splunk"
fileName="${sys:mule.home}${sys:file.separator}logs
${sys:file.separator}ForSplunk.log"
filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}ForSplunk-%i.log">
<PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
<SizeBasedTriggeringPolicy size="10 MB" />
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<AsyncRoot level="INFO">
<AppenderRef ref="file" />
</AsyncRoot>
<AsyncLogger name="test" level="INFO" category="splunk">
<AppenderRef ref="splunk" />
</AsyncLogger>
Logger output for category APP and splunk. In this case i want category splunk logger message to go to file ForSplunk.log.
[[splunktest].HTTP_Listener_Configuration.worker.01] APP: Start
Logger
[[splunktest].HTTP_Listener_Configuration.worker.01] splunk:
End Logger
#user3366906 We had used logging category in our project.
You need to specify some value to category field and that same value should be specified in a cloudHub logging.

Toggling text file logging in log4net

In my Web app I want to have the ability to toggle text file logging on or off, is there a way within log4net for this to be accomplished?
Right now I'm doing this in my log4net.config file, but this makes me think it's going to log in both places all the time:
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<param name="ApplicationName" value="Lending Service" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message %newline %exception" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="mylogfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
<level value="ALL"/>
<appender-ref ref="EventLogAppender" />
</root>
EDIT
I'm looking for a way to do this through a .config setting, whether it's something that needs to be added to the log4net.config or the web.config file.
I don't know about web.config, but if you add the below line into your appender for the log4net.config it should work.
<threshold value="OFF"/>
As long as the value is greater than the entry your sending, it won't show up in the log (i.e. value="WARN" won't log an entry for DEBUG/INFO.) Then you can change it to "OFF" to suppress any logging.

log4net log file is not created

In my project I'm using MVC, NHibernate and for logging html errors I wanna use log4net library.
Here is web.config configuration
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<!--<sectionGroup name="blowery.web">
<section name="httpCompress" type="blowery.Web.HttpCompress.SectionHandler, blowery.Web.HttpCompress"/>
</sectionGroup>-->
</configSections>
<log4net debug="false">
<!-- Define some output appenders -->
<appender name="trace" type="log4net.Appender.TraceAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="console" type="log4net.Appender.ConsoleAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default priority -->
<root>
<priority value="INFO" />
<appender-ref ref="console" />
</root>
</log4net>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">Password=maserver.login12345;Persist Security Info=True;User ID=sa;Initial Catalog=Mashhad4;Data Source=DEVSERVER</property>
<property name="show_sql">true</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<!--<property name="hibernate.batch_size">20</property>-->
<!--<property name="command_timeout">5000</property>-->
</session-factory>
</hibernate-configuration>
<configuration>
In AssemblyInfo.cs file I have this:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
But in the controller, when I use this part of code, it doesn't create any txt file.
log4net.Config.BasicConfigurator.Configure();
Exception inner = filterContext.Exception.InnerException;
log4net.ILog log = log4net.LogManager.GetLogger("MainLogger");
log.Error("Unhandled exception", inner == null ? filterContext.Exception : inner);
What is the problem?
Try adding this under the root element:
<logger name="NHibernate.SQL">
<appender-ref ref="rollingFile" />
<level value="ALL" />
</logger>
Edit:
In order to use the logger called MainLogger, use this:
<logger name="MainLogger">
<appender-ref ref="rollingFile" />
<level value="ALL" />
</logger>
When in Debug mode, the file will be created in <projectfolder>\bin\Debug, since you did not specify a path but only the filename.

How to suppress logging message for nHibernate

I am using log4Net in my application which also uses nHibernate. Now my eventlogger is flooded with nHibernate messages which I want to suppress.
I have tried this but it did not work.
<log4net>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<param name="LogName" value="App Log" />
<param name="ApplicationName" value="DataService" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="NHibernate*" />
<acceptOnMatch value="false" />
</filter>
</appender>
<root>
<priority value="ALL"/>
<appender-ref ref="EventLogAppender"/>
</root>
</log4net>
You can add a logger for nHibernate and set up a different level:
<logger name="NHibernate" additivity="false">
<level value="WARN"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
you should use
<loggerToMatch value="NHibernate.*" /> or <loggerToMatch value="NHibernate" />
not
<loggerToMatch value="NHibernate*" />
you can find details in regex rules