Log4j : How do I lookup the datasource in log4jconf.xml - log4j

I am using weblogic for application deployment and I have created a datasource with jndi name "MyDataSource". when I try to use it in my log4j configuration, it is not working
<appender name="myDbAppender" class="org.apache.log4j.jdbc.JDBCAppender">
<param name="jndiName" value="MyDataSource"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="INSERT INTO LOGGING (user_id,
correlation_id, first_name, last_name, event_name, role,
status, access_level, message, logger, loglevel)
VALUES ( '%X{USER_ID}', '%X{CORRELATION_ID}', '%X{FIRST_NAME}',
'%X{LAST_NAME}','%X{EVENT_NAME}','%X{ROLE}','%X{STATUS}','%X
{ACCESS_LEVEL}',
'%m' , '%X{LOGGER}','%p' )"/>
</layout>
</appender>

If you want to use this feature, you need to add the jar file of Apache Extras for Apache log4j and use the class org.apache.log4j.DBAppender. e.g.:
<!-- console -->
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.SimpleLayout" />
</appender>
<!-- db -->
<appender name="DBOUT" class="org.apache.log4j.DBAppender">
<connectionSource class="org.apache.log4j.receivers.db.JNDIConnectionSource">
<param name="jndiLocation" value="java:/comp/env/jdbc/MySQLDS" />
</connectionSource>
</appender>
<!-- root -->
<root>
<priority value="ALL" />
<appender-ref ref="STDOUT" />
<appender-ref ref="DBOUT" />
</root>

Related

Passing log4net ConfigurationFile to a new AppDomain

What I need to do is get hold of the the :
<param name="File" value=".\MyExe.exe.log"/>
value but when I try to access the appender using the following :-
var rootAppender = ((Hierarchy)LogManager.GetRepository())
.Root.Appenders.OfType<RollingFileAppender>()
.FirstOrDefault();
var myFilePath = rootAppender != null ? rootAppender.File : string.Empty;
I find all I get is null. So how do I access my path?
This is my log4net config section :-
<log4net threshold="All">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value=".\MyExe.exe.log"/>
<param name="AppendToFile" value="true"/>
<param name="ImmediateFlush" value="true"/>
<param name="RollingStyle" value="size"/>
<param name="MaxSizeRollBackups" value="5"/>
<param name="MaximumFileSize" value="1MB"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<logger name="MYLOGGER">
<level value="All"/>
<appender-ref ref="RollingLogFileAppender"/>
</logger>
</log4net>
Thanks
The reason you can't find the file name is that you are querying the root logger - but you don't have a root logger defined in your config, and the default root does not have any appenders linked to it.
You would need to run this code instead which uses your defined logger to get the filename:
var loggerAppender = LogManager.GetLogger("MYLOGGER").Logger.Repository
.GetAppenders()
.OfType<RollingFileAppender>()
.FirstOrDefault();
var myFilePath = loggerAppender != null ? loggerAppender.File : string.Empty;

getAppender return null from xml

when I try to read a appender from XML I get return null,
This is my file. xml
<appender name="FileInfoStatistics" class="utils.monitorstatistics.CustonAppenderRollFile">
<param name="file" value="Statistics.log"/>
<param name="MaxFileSize" value="100KB"/>
<layout class="utils.monitorstatistics.CustomAppenderStatistics">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<logger name="utils.monitorstatistics" additivity="false">
<level value="INFO"/>
<appender-ref ref="FileInfoStatistics"/>
</logger>
and this is the part java where I try to read the appender:
Logger mylogger = Logger.getLogger("utils.monitorstatistics");
Appender app = mylogger.getAppender("FileInfoStatistics");
getAppender return null

configure log4j file

I have this log4j.config
<appender name="FileInfoStatistics" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${log.dir}/T_Statistics.log" />
<param name="MaxFileSize" value="8000KB"/>
<!-- Keep one backup file -->
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %5p %c{1} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="INFO" />
</filter>
</appender>
but I would like just when then file T_Statistics.log is created firt time and one line as e.g This a log file, Is this possible from log4j.config?
I saw this is possible en log4net with:
<param name="Header" value="xxxx"
Is there something like in log4j?
Well, After some research I found a solution to my problem, said if someone would be useful.
I have created a custom appender:
package com.mycompany.logger;
import org.apache.log4j.PatternLayout;
public class MyPatterLayout extends PatternLayout
{
#Override
public String getHeader()
{
return "message id, file name, start time, end time, status" + System.getProperty("line.separator");
}
}
And then I have configured my log4j.xml:
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="com.mycompany.logger.MyPatterLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
And this is the result:
message id, file name, start time, end time, status
DEBUG App - Here is some DEBUG
INFO App - Here is some INFO
WARN App - Here is some WARN
ERROR App - Here is some ERROR
FATAL App - Here is some FATAL

Can the log file path be changed on the fly (reguarly)

I've been experimenting with log4net as it appears that it does not support a particular logging feature my project needs. In short, I want to be able to control the log file path in code. This path will change constantly. The specific use case is a set of file system watchers, and a separate log file is required per instance.
Can this be done?
I want to be able to specify a variable that controls the logging destination in code.
For example:
var log4NetLogger1 = new Log4NetLogger("LogFileAppender1");
log4NetLogger1.InformationEvent("Log message 1");
var log4NetLogger2 = new Log4NetLogger("LogFileAppender2");
log4NetLogger2.InformationEvent("Log message 2");
In the above example, I am passing a string to the log4net wrapper, that matches an appender name in configuration. The idea is that the log4net wrapper changes the logging target by modifying the appender in use via GetLogger.
_log = log4net.LogManager.GetLogger(appenderName);
The result is that two different log files are created, but the log message is written to both. It appears that some aspect of the log4net configuration is global, and I am not able to change log path on the fly in this way.
Configuration file:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<appender-ref ref="LogFileAppender1"/>
<appender-ref ref="LogFileAppender2"/>
</root>
<appender name="LogFileAppender1" type="log4net.Appender.RollingFileAppender">
<threshold value="INFO"/>
<param name="File" value="C:\Tmp\EDP_TEST\LOG_DESTINATION\TestLog1.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
</appender>
<appender name="LogFileAppender2" type="log4net.Appender.RollingFileAppender">
<threshold value="INFO"/>
<param name="File" value="C:\Tmp\EDP_TEST\LOG_DESTINATION\TestLog2.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
</appender>
</log4net>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
Full wrapper class:
using System;
using log4net;
namespace Log4NetRunner
{
public enum LoggingLevel
{
Information,
Warning,
Error
}
public class Log4NetLogger
{
private readonly ILog _log;
public Log4NetLogger(Type type)
{
if (_log == null)
{
_log = log4net.LogManager.GetLogger(type);
}
}
public Log4NetLogger(string appenderName)
{
if (_log == null)
{
_log = log4net.LogManager.GetLogger(appenderName);
}
}
public void FatalErrorEvent(string messageText)
{
SendLog(messageText, LoggingLevel.Error);
}
public void WarningEvent(string messageText)
{
SendLog(messageText, LoggingLevel.Warning);
}
public void InformationEvent(string messageText)
{
SendLog(messageText, LoggingLevel.Information);
}
private void SendLog(string messageText, LoggingLevel logLevel)
{
ILog logger = _log;
switch (logLevel)
{
case LoggingLevel.Error:
logger.Error(messageText);
break;
case LoggingLevel.Warning:
logger.Warn(messageText);
break;
case LoggingLevel.Information:
logger.Info(messageText);
break;
default:
logger.Error("Unknown Logging level: " + messageText);
break;
}
}
}
}
Yes it can, you just need to use the loggers section of the config.
http://www.beefycode.com/post/Log4Net-Tutorial-pt-5-Using-Logger-Objects.aspx
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<!--<appender-ref ref="LogFileAppender1"/>
<appender-ref ref="LogFileAppender2"/>-->
</root>
<logger name="Logger1">
<level value="ALL" />
<appender-ref ref="LogFileAppender1" />
</logger>
<logger name="Logger2">
<level value="ALL" />
<appender-ref ref="LogFileAppender2" />
</logger>
<appender name="LogFileAppender1" type="log4net.Appender.RollingFileAppender">
<threshold value="INFO"/>
<param name="File" value="C:\Tmp\EDP_TEST\LOG_DESTINATION\TestLog1.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
</appender>
<appender name="LogFileAppender2" type="log4net.Appender.RollingFileAppender">
<threshold value="INFO"/>
<param name="File" value="C:\Tmp\EDP_TEST\LOG_DESTINATION\TestLog2.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
</appender>
</log4net>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

Is it possible to use Log4Net to write to log file within a CLR Stored Procedure?

Issue: Unable to debug (write) to a Log File using Log4net inside of a CLR Stored Procedure. Possibly a problem with the way I'm building the CLR project? I'm only importing the DLL's into sql server (create assembly....). Do I need to import the App.Config as well?
DLL Name:
CLRTest.dll
Source Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using log4net;
using log4net.Config;
public class MyClass
{
private readonly static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static readonly string sContextConn = "Context Connection=true";
[Microsoft.SqlServer.Server.SqlProcedure]
public static void Select1()
{
XmlConfigurator.Configure();
log.Debug("Begin Select1()...");
using (SqlConnection connection = new SqlConnection(sContextConn))
{
connection.Open();
SqlCommand command = new SqlCommand("select 1", connection);
SqlDataReader r = command.ExecuteReader();
SqlContext.Pipe.Send(r);
}
log.Debug("End Select1()...");
}
}//end MyClass
Log4Net XML Config (App.Config):
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\log\clrsql.log" />
<param name="AppendToFile" value="true" />
<datePattern value="yyyyMMdd-HHmm" />
<param name="rollingStyle" value="Size" />
<param name="maxSizeRollBackups" value="50" />
<param name="maximumFileSize" value="25MB" />
<param name="staticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [t-%t] [%c.%M(%L)] %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %-5p [%c.%M(%L)] %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
Permissions on SQL Server:
C:\log\ --
NETWORK SERVICE and MyDomain\sqlserveraccount have Full Control on the "log" folder.
SQL Scripts:
drop procedure clr_Select1
go
drop assembly CLRTest
go
create ASSEMBLY CLRTest FROM 'C:\Share\ClrSql\TEST\CLRTest.dll' WITH PERMISSION_SET = unsafe
go
CREATE PROCEDURE clr_Select1
AS EXTERNAL NAME CLRTest.MyClass.Select1
go
exec clr_Select1
SQL Output:
(No column name)
1
The log4net configurator call that you're making actually tries to find the AppDomain.CurrentDomain.SetupInformation.ConfigurationFile. I don't know what the AppDomain is for a stored procedure.
You might want to try a standalone configuration file and use the overload of that configurator.
log4net.Config.XmlConfigurator.Configure(new FileInfo("config.log4net"));

Resources