Log4Net properties is overwriting for simultaneous requests - asp.net-mvc-3

I am trying to write log file per user separately
For this I am setting the property of file name as a current user id and configuring log4net like this
log4net.GlobalContext.Properties["userid"] = userId;
log4net.Config.XmlConfigurator.Configure();
This is all working good, it creates new log file per user and logs all the activities by the user in their separate files.
Problem starts when multiple users request service simultaneously, it then starts putting part of logs of one user to another users file, so all logs get mixed up in the simultaneous users files.
I am getting the reason that global settings are getting overridden by user2 while user1 is in progress so user1 starts writing logs to user2's file as the settings are now overridden.
I tried log4net.LogicalThreadContext and log4net.ThreadContext also but they also didn't work.
Is there any workaround for this?

Log4net has only one configuration. So you can't fix this by getting a different configuration for each user. However you can log the username, or create a logger named after your user. Then you can make different appenders for each user, and log the output to different files. And you only have to call log4net.Config.XmlConfigurator.Configure(); once.
string userid = (...) ;
ILog log = LogManager.GetLogger(userId);
<log4net>
<appender name="<userid1>" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
....
</appender>
<appender name="<userid2>" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
....
</appender>
<logger name="<userid1>">
<level value="ALL"/>
<appender-ref ref="<userid1>"/>     
</logger>
<logger name="<userid2>">
<level value="ALL"/>
<appender-ref ref="<userid2>"/>     
</logger>
....

Related

Recursive logging from [org.apache.Kafka.clients.networkclient] for appender

I have a spring boot application using log4j2 for logging and want to use Kafka appender . Mentioned all properties in right format in log4j2.xml and properties of Kafka topic ,server and groupid in application.properties
But each time I start my application am facing a error in console logs saying as below
Kafka-producer-network-thread | producer-1 WARN recursive logging from [org.apache.Kafka.clients.networkclient] for appender [mykafkaappendername]
This warning means you should configure your org.apache.kafka Logger to send log events to some other Appender.
You should probably have a proper Log Appender defined and should refer from your logger with appropriate log level and additivity, something like this
<logger name="com" level="INFO" additivity="false">
<appender-ref ref="appenderName"/>
</logger>
<root level="logLevel">
<appender-ref ref="appenderName"/>
</root>

Logback configuration issue

I'm using Spring Boot 2.3.2. I'm trying to configure logback to continue outputing to the console EXACTLY how it does today (ansi and all), plus mirror it to a log file in the exact same format (ansi and all). I want ALL the log files to be in the format of ${spring.application.name}.YYYY-MM-DD.log including the active one and I want to keep 7 days worth of files, so I've got it configured like this:
application.properties:
logging.file.name=${spring.application.name}
logging.file.path=/var/logs
logging.pattern.file=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx
logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<timestamp key="byDate" datePattern="yyyy-MM-dd" />
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/${LOG_FILE}.${byDate}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
This results in a collision error between file and fileNamePattern. I get what the error means and I can fix it by removing the date stamp from file, but there is no way to have all the files in a consistent format?
If I remove the timestamp from the active file, then it works except for the next issue...
The other issue that I have is that the logfile never gets written too until I shutdown the application.
This results in a collision error between file and fileNamePattern. I
get what the error means and I can fix it by removing the date stamp
from file, but there is no way to have all the files in a consistent
format?
Here :
<fileNamePattern>${LOG_PATH}/${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
you don't have to specify ${LOG_PATH}. It is a filename pattern not a full path.
That should be that :
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
The other issue that I have is that the logfile never gets written too
until I shutdown the application.
Not sure but it looks a file locking. Try to ensure that no process locks the file.
As an additional remark : you specified :
logging.file.name=${spring.application.name}
logging.file.path=/var/logs
But the spring boot logging doc specify to reference the one or the other :
you need to set a logging.file.name or logging.file.path property (for
example, in your application.properties).
Valuing logging.file.name such as the following respects better the documentation :
logging.file.name=/var/log/${spring.application.name}

Spring boot - how to reduce log output from specific library?

I am using the library net.iakovlev.timeshape.TimeZoneEngine and it is spewing a lot of [DEBUG] messages on startup which seems to be the default behaviour.
In application.yml I should be able to control logging level with but that's not working somehow.
logging:
level:
net.iakovlev.timeshape: ERROR
Isn't this the correct way to do so?
Set your log configuration file, and add something like this :
...
<logger name="net.iakovlev.timeshape" level ="ERROR" additivity="false">
<!-- YOUR APPENDER -->
<appender-ref ref="CONSOLE" />
</logger>
...

Setting level to a logger in spring-boot

In logback-spring.xml file of a project that is new to me I see this configuration
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
......
</appender>
<logger name="com.myproject.myclass" level="${APP_LOGGING_LEVEL:-INFO}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
As far as I understand, all logs from com.myproject.myclass class will be sent to <appender name="STDOUT"> that is console.
Could someone explain me what will be the level of this log with this config level="${APP_LOGGING_LEVEL:-INFO}"? In documentation I see only constants like WARN, DEBUG, etc
In Spring ${APP_LOGGING_LEVEL:-INFO} means APP_LOGGING_LEVEL is a dynamic variable. The value for which can be set from properties file or from command line when running the project or from any other place accessible. If not set whatever after : is selected.
In your case if not set INFO is selected

How to make Playframework Evolutions logging more verbose?

I've been trying to see what's going on with them, as I'm facing a problem.
When I click the "apply this script" button, the page asking me to apply the script appears again, and it stucks in that loop. It creates the "play_evolutions" table, but no script is run.
The log in the console doesn't give me any relevant info. I've run manually the script and it's ok... So I wan't to know what the real problem is, therefore I need more logging.
Evolutions are not very verbose in general (when it talks it mostly does that on warn level).
Basically make sure you have this line at you should get everything play-evolutions produce (it should be in logback.xml file):
<logger name="play.api.db.evolutions" level="DEBUG" />
Full file (as an example only):
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%level %logger{15} - %message%n%xException</pattern>
</encoder>
</appender>
<logger name="play.api.db.evolutions" level="DEBUG" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

Resources