how to switch log4net multiple log files using C# code? - log4net

I'm using log4net, I would like to have 2 logs,
- BasketballCustomer.log, for all Customers that plays Basketball;
- ChessCustomer.log, for all Customers that plays Chess.
, while for each customer, whether he/she plays Basketball or Chess, is only known until runtime.
I would like to have each log configured separately, about log file name, size, number, log level, etc.
Also, I'd prefer such set up done by C# code, not config file.
How could I do that?
I tried search on net, there are some articles but none meet exactly my requirements
- Log4Net and multiple log files talked about multiple log files but it does not toggle during runtime;
- Configure Log4net to write to multiple files is similiar but it's done in config file....
Please kindly suggest, many thanks!

You can do this by using an environment variable in the log4net.config and then set the value of the environment variable through the C# code
So somewhere in your C# class, do something like:
Environment.SetEnvironmentVariable("log_file_name", "MyLogFileName");
And then in the log4net.config that is used, specify the value to the name of the environment variable. The syntax would be something like this:
<param name="File" value="${log_file_name}".log/>

Related

Cannot change zookeeper log filename

Zookeeper is creating the logs with a name zookeeper-root-hostname.out but this is my log4j.properties:
zookeeper.root.logger=INFO, CONSOLE
zookeeper.console.threshold=INFO
zookeeper.log.dir=.
zookeeper.log.file=zookeeper.log
zookeeper.log.threshold=INFO
zookeeper.log.maxfilesize=256MB
zookeeper.log.maxbackupindex=20
zookeeper.tracelog.dir=${zookeeper.log.dir}
zookeeper.tracelog.file=zookeeper_trace.log
log4j.rootLogger=${zookeeper.root.logger}
#
# console
# Add "console" to rootlogger above if you want to use this
#
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold}
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}#%L] - %m%n
#
# Add ROLLINGFILE to rootLogger to get log file output
#
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold}
log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}
log4j.appender.ROLLINGFILE.MaxFileSize=${zookeeper.log.maxfilesize}
log4j.appender.ROLLINGFILE.MaxBackupIndex=${zookeeper.log.maxbackupindex}
log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}#%L] - %m%n
If a change the property zookeeper.log.file=zookeeper.log the file is created with the same name, How can I change the filename of the log?
Update
I found that the file zkServer.sh set the variable ZOO_LOG_FILE and overrides the value that is define in the log4j.properties:
ZOO_LOG_FILE=zookeeper-$USER-server-$HOSTNAME.log
I can modify this file but Is it ok to change that manually?
In typical Java application fashion the logging situation with Zookeeper is incredibly complicated, custom, and poorly documented. The official Administrators manual logging section is a tiny paragraph with about zero information, but suggests you can rely on your existing knowledge of the third party library log4j configured to proxy through the other third party library SLF4J. This actually wouldn't be too bad since that complicated arrangement is common as many popular open source projects ignore the built-in logging framework that ships will Java, but later you'll discover using the log4j.properties file in the conf directory actually doesn't work as expected because custom properties are used in the provided start up script (zkServer.sh) that override many things.
In order to set the file name note the file conf/log4j.properties contains the following variable assignment that is overridden by the start up script (zkServer.sh):
zookeeper.log.file=zookeeper.log
Either set the environment variables externally, modify the start up script or update the log4j.properties to not use the variable:
#log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}
log4j.appender.ROLLINGFILE.File=/the/actual/path/goes/here.log
You may also want to enable the ROLLINGFILE like so:
#log4j.rootLogger=${zookeeper.root.logger}
log4j.rootLogger=INFO,ROLLINGFILE
After searching I found this link but I didn't found how to change the variable for the log, I noticed that inside the file zkServer.sh is defined the name of the file, so I needed to change this variable:
_ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper-$USER-server-$HOSTNAME.out"
I don't know if it's ok to change that variable but now is working as expected.

Using log4j for different applications

I have a problem with log4j loggin and I hope you can help me on this.
This is the scenario: I have 3 different applications (in other words 3 .jar) A, B and C. A is always running and from time to time calls B and C to execute them. Each of them have its own log4j.properties (with the path duly included in the MANIFEST) but Log4j only takes the properties of the main app A into consideration, ignoring the rest. I need to know if I can force Log4j to use for each app its own properties. I have seen something related using DOMConfigurator, but if I'm not wrong this only works with xml files and I'm using properties.
Thanks in advance
There are several Configurators, one of which is the PropertyConfigurator.
I need to know if I can force Log4j to use for each app its own properties.
Yes, you can. When you launch the applications B and C, define this system variable at their command line:
-Dlog4j.configuration=B.properties
where B.properties is the file name of log4j properties file in B's classpath. Likewise for application C.
Edit: found this: Change location of log4j.properties

Splunk rewrites xml input incorrectly

I have a number of applications that I want to log to Splunk. I will be sending the data in an XML format via a UDP listener. The data that is being sent looks like:
<log4j:event logger="ASP.global_asax" level="INFO" timestamp="1303830487907" thread="15">
<log4j:message>New session started</log4j:message>
<log4j:properties>
<log4j:data name="log4japp" value="4ef113dd-9-129483040292873753(4644)" />
<log4j:data name="log4jmachinename" value="W7-SUN-JSTANTON" />
</log4j:properties>
</log4j:event>
However when it is processed by Splunk it appears like:
Apr 26 16:18:09 127.0.0.1 <log4j:message>New session started</log4j:message><log4j:properties><log4j:data name="log4japp" value="4ef113dd-9-129483040292873753(4644)"/><log4j:data name="log4jmachinename" value="W7-SUN-JSTANTON"/></log4j:properties></log4j:event>
Basically it looks like Splunk looks like it has overwritten the opening node, and as a result lossing the log level data, with the datetime that it received it. The applications that are sending it are using nLog with a log4j type target (with an Log4JXmlEventLayout layout). I have configured the sourcetype as log4jxml (custom name) but I think I need to tell it not to do something with the date/time field in the props.conf file (but not too sure what that something is).
I am also using the windows version of Splunk so the file paths are slightly different to the online manuals.
Any help would be most welcome.
It turns out I was doing 2 things wrong (maybe more but I have not found thoses yet)
In the inputs.conf file I need to add the following to my input definition:
no_priority_stripping = true
no_appending_timestamp = true
The second thing I was doing wrong was to put these files in
C:\Program Files\Splunk\etc\system\local\
when they SHOULD have been put in
C:\Program Files\Splunk\etc\apps\search\local\
I hope that this helps somebody else out

How do I create a new file for each log4net logger instance?

I am trying to create a different log file for each thread (the threads are handling some site processing). Essentially just naming the log file with a name that is derived from the site that is being processed.
I know all about the GlobalContext.properties and also the TheadContext.properties and neither really seem to work for what I am trying to do. In pretty much either case any concurrently running threads just use whichever file is the current output file as their output. The only difference I have seen is that if I use the ThreadContext to set the property it makes all the files but only outputs to one, where using global it seems to only make the one file unless the processes start at different times.
What I would REALLY like to do is be able to tell the file to just use the name of the logger object (the name that is used in instantiating the object) in the file name instead of using these properties.
Instead of fighting the configuration, I recommend that you rethink your logging strategy. log4net was not built for a log file per thread/class. Loggers are created at startup or on first write (depends). Use a tailing log file reader, like Kiwi Log Viewer or Splunk, and filter on the thread id or logger name in your messages.
If you put them in your conversion pattern...
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline" />

log4 net Dynamic file name assigning

I want to know how to dynamically assign file name using Log4net .My application is such that 10 different files should be dynamically created based on user input ,and later based on the name the corresponding file name needs to be picked up and information written to it
For example in my application based on my buisness requirement for every xml file a corresponding log file with the same name as xml file should be created .Later whenever I do any modification to the xml file an entry needs to be in the corresponding log file
Please help . I having trouble to get control of the appropriate log to write it
Have not done this, but there are probably a number of ways of doing this, so this may not be the best way, but it should work
public OpenLogFile(string fileName)
{
log4net.Layout.ILayout layout = new log4net.Layout.PatternLayout("%d [%t]%-5p : - %m%n");;
log4net.Appender.FileAppender appender = new log4net.Appender.FileAppender(layout , filename);
appender.Threshold = log4net.Core.Level.Info;
log4net.Config.BasicConfigurator.Configure(appender);
}
Then just call OpenLogfile when you need to switch files.
You might need to tweak the layout or appender type.
A big disadvantage of this method is you losing the xml configuration and the ability to change settings at runtime. So a better way might be to configure your appender in the xml file to use a property
eg
file type="log4net.Util.PatternString" value="Logfiles\Log_For_%property{MyLogFileName}"
Then in your code you could change the property
log4net.GlobalContext.Properties["MyLogFileName"] = ...;
The tricky bit is to get log4net to reload itself. I haven't read the documentation of this, so I don't know if there is a way of forcing a reload. It might work if you just call log4net.Config.XmlConfigurator.ConfigureAndWatch again. Otherwise it should work if you opened the xml file and saved it again (without needing to change anything)
Hope this helps.

Resources