log4net items not shown in AppServiceAppLogs - azure

We are using log4net for our application logging in azure.
We configured it according to this document , the logs are stored in
d:/home/LogFiles/Application/Log4netfile-[%processid].txt
we can download the files from https://xxxx.scm.azurewebsites.net/api/dump and I can also see them in Log stream, there it looks like this
2022-02-06T18:22:57 PID[29616] Verbose log4net: Adding appender named [FileAppender] to logger [root].
2022-02-06T18:22:57 PID[29616] Verbose log4net: Hierarchy Threshold []
2022-02-06T18:22:58 PID[29616] Verbose LOG4NET Fatal LOG4NET LOG4NET
2022-02-06T18:22:58 PID[29616] Verbose LOG4NET Debug Message Message
Line items like:
log4net: Adding appender named
are created by log4net during internal setup (using debug=true), and those show up in our AppServiceAppLogs, however, items which are genereted by the logger using:
Logger.Fatal("LOG4NET Fatal LOG4NET LOG4NET");
are visible in stream and files but they are not showing up in AppServiceLogs.
I tried multiple different appenders the only one which shows up in Log-Streams and in files is FileAppender (or RollingFileAppender), all others are not showing in LogStreams or (as expected) in the files.
Based on this https://learn.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#send-logs-to-azure-monitor I assume that everything writen to the files should be than sent to azure-monitor - but in our case this is not hapening. Could you provide some reference about implementation / code how the sending of the files is done, and which process does that ?
I am wandering is there anything which we could configure differently to get the log-items to end up in AppServiceLogs? Or should we use some other appender?
I am aware of the issue in AspNetTraceAppender and I am trying to avoid implementation of new appender.

There are a few things you can look into:
Call log4net configure before writing to your log file (only once is enough):
log4net.Config.XmlConfigurator();
or
log4net.Config.XmlConfigurator.Configure();
and then you can add flushing to your configuration:
<appender name="AzureTraceAppender" type="log4net.Appender.TraceAppender">
<param name="ImmediateFlush" value="true" />
<layout type="log4net.Layout.PatternLayout">
<!-- can be any pattern you like -->
<conversionPattern value="%logger - %message" />
</layout>
</appender>
Immediately, this will flush the message.
In Global.asax, To make log4net read your configuration.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
// Initialize log4net.
log4net.Config.XmlConfigurator.Configure();
}
Make sure Azure Diagnostics has configured to all information needed for debugging.
After that, you can enable internal log4net debugging. Refer internal debugging on this log4net faq page. Standard it should log to your listener you have configured. To the trace element Add the autoflush="true" option . Or find directory on the worker role you can write to and access to read your logs.

Related

How to configure logging level for JAWR using log4j.xml

Currently on our project we are seeing some DEBUG statements printed on our tomcat logs such as this:
12:09:00.824 [localhost-startStop-1] DEBUG n.j.w.r.h.b.AbstractResourceBundleHandler - Storing a generated bundle with an id of:/script/lib/itegration.dataTables.bootstrap.js 12:09:00.850 [localhost-startStop-1] DEBUG n.j.w.r.h.b.AbstractResourceBundleHandler - Storing a generated bundle with an id of:/script/lib/itegration.dataTables.bootstrap.js
We have determined that the debug statements that are being logged are caused because the condition LOGGER.isDebugEnabled() in the JAWR AbstractResourceBundleHandler.java class is always true. The snip of the code that logs is as follows:
if (LOGGER.isDebugEnabled()) {
String msg = "Storing a generated "
+ (gzipFile ? "and gzipped" : "")
+ " bundle with an id of:" + bundleName;
LOGGER.debug(msg);
}
As you can see there is a LOGGER.isDebugEnabled() which is always true. Is there a way to configure the logging level for this package/class/jar? We are currently using log4j as our logging framework and have tried configuring this package to log only INFO level but that has not worked. We also have the jawr.debug.on property set to false (jawr.debug.on=false)
I understand that JAWR is using slf4j for their logger and there is a way to use the slf4j bridge to log4j which we have configured, we just cannot get the logging level configuation for this package to work.
Does anyone know a way to set the LOGGER.isDebugEnabled() set to false so that these DEBUG log statements do not appear in our tomcat logs?
We are using log4j.xml not a properties file and would like to keep using the .xml configuration if possible.
Thank you
Adding this to our log4j.xml configuration file
<category name="net.jawr">
<priority value="INFO" />
</category>
works fine.

Using Syslog's unix socket with Log4J2

I would like to send the logs of an application on a rsyslog server using the client protocol : Unix socket.
Both application and rsyslog server are on the same machine.
I've been comparing different logging tools : Log4J, Logback and Log4J2.
Log4J's Syslog appender doesn't permit it natively. A solution is to use Syslog4J and configure our Syslog4JAppender this way :
<appender name="mySyslogAppender" class="org.productivity.java.syslog4j.impl.log4j.Syslog4jAppender">
<param name="protocol" value="unix_socket" />
...
</appender>
Same as Log4J, Logback's Syslog appender doesn't permit it natively. A solution is to use Syslog4J and logback-syslog4j tool, and configure our appender this way :
<appender name="mySyslogAppender" class="com.papertrailapp.logback.Syslog4jAppender">
<syslogConfig class="org.productivity.java.syslog4j.impl.unix.socket.UnixSocketSyslogConfig">
...
</syslogConfig>
</appender>
Now I'm looking for a solution for Log4J 2, but I didn't find any.
Do you know one ?
Do these tools will include natively this functionality ?

Robot Framework: log messages sent through log4j's ConsoleAppender are not visible in output

I use Robot Framework with custom keywords implemented in java libraries. Messages written directly to System.out from my java classes are visible in the robot output - as promised in documentation. However, since the keyword implementations are reusable components, independent from robot framework, I wanted to have more flexible logging there and not using System.out directly. I thought if I redirect my log output to System.out (with log4j's ConsoleAppender), the messages will be visible in robot output. Unfortunately it does not work.
My log4j properties file:
log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Stdout.Target=System.out
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.conversionPattern=%d %-5p [%t] %F:%L %m%n
log4j.appender.Stdout.ImmediateFlush=true
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=mylog.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%d %-5p [%t] %F:%L %m%n
log4j.rootLogger=INFO,Stdout,FA
The file appender works fine, the log file is created and contains all log messages, but the same messages are not visible either on console or in robot output reports. When I run my components without robot framework, the same config works for console as well.
Do you have an ideas what is wrong with the above configuration? Or any other suggestions to have robot logs while avoiding the direct usage of System.out from my java classes?
By default, the ConsoleAppender uses a saved reference to System.out or System.err. To override this behavior, there is a follow property you must set to true.
Try adding this to your log4j.properties file:
log4j.appender.Stdout.follow=true

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.

Does Log4Net Http Appender exist?

Is there an appender in log4net that can allow a winform client to read a log4net log on another server without using a share? My application is hosted as a web service. I'm looking for an HTTP appender or something similar.
There is a GitHub project called PostLog that is a HttpAppender for log4net.
I think you could use the Remoting Appender, something like this:
<appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" >
<sink value="http://localhost:8080/LoggingSink" />
<lossy value="false" />
<bufferSize value="95" />
<onlyFixPartialEventData value="true" />
</appender>
According to the docs:
This Appender is designed to deliver
events to a remote sink. That is any
object that implements the
RemotingAppender.IRemoteLoggingSink
interface. It delivers the events
using .NET remoting. The object to
deliver events to is specified by
setting the appenders Sink property.
There is also a UdpAppender and there is this open source client that can receive these messages:
http://log2console.codeplex.com/

Resources