Is <param> tag ever supposed to work with <logger> or <root>? - apache

From this Apache documentation page, I understand that the <param> tag is supposed to work with the <logger> tag, but apparently it doesn't.
For example, this:
<logger name="LogTest">
<level value="INFO"/>
<appender-ref ref="FileAppender"/>
<param name="File" value="f:\dev\logfile"/>
</logger>
...produces the following error:
XmlHierarchyConfigurator: Cannot find Property [File] to set object on [log4net.Repository.Hierarchy.DefaultLoggerFactory+LoggerImpl]. Source: log4net.Repository.Hierarchy.XmlHierarchyConfigurator.
What am I doing wrong?

Related

Nlog filter when condition with equals not working

I have to pass the variable value from my startup class
LogManager.Configuration.Variables["environment"] = "Development";
I have added below filter in my nlog.config file
<rules>
<logger name="*" minlevel="Error" writeTo="logfile">
<filters>
<when condition="equals('${var:environment}', 'Development')" action="Ignore" />
</filters>
</logger>
</rules>
Even though I pass the value as Development, the message is still getting logged instead of ignore.
However, when I hardcoded the value it's working
You have found a bug in NLog, but it should work if you do this (Will also be faster):
<rules>
<logger name="*" minlevel="Error" writeTo="logfile">
<filters defaultAction='log'>
<when condition="'${var:environment}' == 'Development'" action="Ignore" />
</filters>
</logger>
</rules>
Notice you can also use Layout in minLevel. Ex. minLevel="${var:EnvironmentMinLevel:whenEmpty=Error}", which is much faster than <filters>.
NLog.LogManager.Configuration.Variables["EnvironmentMinLevel"] = "Off";
NLog.LogManager.ReconfigExistingLoggers();
See also https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules
See also: https://github.com/NLog/NLog/wiki/Environment-specific-NLog-Logging-Configuration

How to add HTTP Headers Logback Logstash

I read through the following documentation but cannot figure out what other configuration I need: https://github.com/logstash/logstash-logback-encoder#header-fields
My config file:
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<customFields>{"app_name":"${APP_NAME:-N/A}","app_version":"${APP_VERSION:-N/A}","hostname":"${HOST:-N/A}","environment":"${environment:-${ENVIRONMENT:-N/A}}"}</customFields>
<includeContext>false</includeContext>
<timeZone>UTC</timeZone>
</encoder>
<encoder class="net.logstash.logback.encoder.LogstashAccessEncoder">
<fieldNames>
<requestHeaders>request_headers</requestHeaders>
</fieldNames>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE_ROTATION:-${LOG_FILE}%d{yyyy-MM-dd}.%i}</fileNamePattern>
<maxHistory>${LOG_FILE_ROTATION_MAX_HISTORY:-1}</maxHistory>
<totalSizeCap>${LOG_FILE_ROTATION_TOTAL_SIZE_CAP:-3GB}</totalSizeCap>
<maxFileSize>${LOG_FILE_ROTATION_MAX_FILE_SIZE:-1GB}</maxFileSize>
</rollingPolicy>
</appender>
<root level="${LOG_LEVEL:-INFO}">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
Error:
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.spi.Interpreter#19:29 - no applicable action for [requestHeaders], current ElementPath is [[configuration][appender][encoder][fieldNames][requestHeaders]]
The xml configuration file you provided appears to be for logback-classic (since it includes <root level=...).
Automatic logging of request headers by logstash-logback-encoder is only available for IAccessEvents logged via logback-access.
In other words, automatic logging of request headers is not available for ILoggingEvents logged via a Logger from logback-classic. However, you can manually include them when using logback-classic as described below.
Logging of request headers with logback-access
To log request headers for IAccessEvents logged via logback-access, follow the instructions for setting up logback-access for tomcat or jetty, and add the following to your logback-access.xml. (Note this is not the logback.xml file used by logback-classic)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashAccessEncoder">
<fieldNames>
<requestHeaders>request_headers</requestHeaders>
</fieldNames>
</encoder>
</appender>
<appender-ref ref="console"/>
</configuration>
This configuration was tested using logstash-logback-encoder 5.3. Note that the xml element names are different in logstash-logback-encoder versions prior to 5.0, so ensure you are using 5.0+ with that configuration.
Logging of request headers with logback-classic
logstash-logback-encoder does not provide first-class support for logging request headers via logback-classic. However, you include them in a log event logged via a Logger by using event-specific custom fields.
For example, in a class that has access to the http request (such as a servlet filter), you could do something like this:
Map<String, String> httpHeadersMap = ...; // get http request headers as a map
LOGGER.info("request", StructuredArguments.entries(httpHeadersMap));
and configure a LogbackEncoder in your logback.xml like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<root level="info">
<appender-ref ref="console" />
</root>
</configuration>

How to write soapUI groovy script log into text file

I have to write Groovy script log in to a text file, I am able to write request and response of any Soap step using context.expand.
To write test request and response I am using:
def request = context.expand('${SoapRequest#Request}')
new File ("D:/RequestFile.txt").write(request)
What I want is to save the log output in a file:
Suggest me a way to write Groovy script log in a text file.
Config way log4j.xml
There is already a file for Groovy log configured in SOAPUI log4j configuration file.
In SOAPUI_HOME\bin\soapui-log4j.xml:
<appender name="GLOBAL_GROOVY_LOG" class="org.apache.log4j.FileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="File" value="${soapui.logroot}global-groovy.log"/>
<param name="Threshold" value="DEBUG"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<logger name="groovy.log">
<level value="INFO" />
<appender-ref ref="GLOBAL_GROOVY_LOG" />
</logger>
A possible way is to add another custom FileAppender in this config file. If you change this file remember to restart SOAPUI in order that it can load the changes.
Something like this can do the trick:
<appender name="GLOBAL_GROOVY_LOG" class="org.apache.log4j.FileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="File" value="${soapui.logroot}global-groovy.log"/>
<param name="Threshold" value="DEBUG"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<appender name="MYLOG_CUSTOM" class="org.apache.log4j.FileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="File" value="/absoultePath/yourlogFile.txt"/>
<param name="Threshold" value="DEBUG"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<logger name="groovy.log">
<level value="INFO" />
<appender-ref ref="GLOBAL_GROOVY_LOG" />
<appender-ref ref="MYLOG_CUSTOM" />
</logger>
Dinamically using Groovy
However it seems that you want to do it dynamically using Groovy script, hence you can use the follow code to get <logger name="groovy.log"> and add a FileAppender to it; in order that you can save the logs in a custom file:
import org.apache.log4j.Logger
import org.apache.log4j.PatternLayout
import org.apache.log4j.RollingFileAppender
// get the groovy logger by name
def groovyLogger = Logger.getLogger('groovy.log')
// pattern Layout
PatternLayout layout = new PatternLayout("%d{ISO8601} [%t] %-5p %c %x - %m%n")
// create a file appender
RollingFileAppender fileAppender = new RollingFileAppender(layout, "/absolutePath/myLog.txt")
groovyLogger.addAppender(fileAppender)
log.info 'someText to the logger'
NOTE: If you don't use an absolute path in FileAppender the log file is saved relative to *SOAPUI_HOME\bin*
When you configure this the rest of logs in Groovy testSteps will be appended to this file. If you want to get only the log for a specific Groovy testStep then you can simply remove the appender at the end of the script:
groovyLogger.removeAppender(fileAppender);
UPDATE:
From you comment: I want to store output for each run, and if I am executing this again it should rewrite the file.
So you want to store output for each run... but you want to overwrite the file if you execute again? This is contradictory isn't?
If you want to overwrite the file instead of append the content you can use setAppend(false):
fileAppender.setAppend(false)
fileAppender.activateOptions()
groovyLogger.addAppender(fileAppender)

log4net dynamic LogFileLocation

I have log file location in web.config as follows.
<appSettings>
<add key="LogPath" value="D:\Service\"/>
</appSettings>
I have log4net config in web.config as follows
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%property{LogName}"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m%n"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
Initialized GetLogger at the top of the Service class as follows
Private Shared ReadOnly log As ILog = LogManager.GetLogger(GetType(Service))
I have created GlobalContext property for LogName as follows
log4net.GlobalContext.Properties("LogName") = ConfigurationManager.AppSettings("LogPath") & "Service." & Format(Now, "ddMMyyyy") & ".txt"
Logging is done with log.Info as follows
log.Info(strComments)
In this scenario, the log file is created as "null" under Project folder and logging the details.
I want the log location to be "D:\Service\Service.30082012.txt" according to the code above.
What am I missing? Please help me out.
Typically I separate the log4net configuration from my web.config to avoid web application restarts ( How to prevent an ASP.NET application restarting when the web.config is modified? ) when you change the web.config file.
I usually just have a log4net.config file to store all of these settings and I configure it to watch the config file for changes:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Also I'm not quite sure why you are storing your log file directory outside of the log4net configuration unless it's just easier for you to modify. I typically keep the path in the log4net config like so:
<appender name="File" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="${ALLUSERSPROFILE}/CompanyName/Logs/ApplicationName/%date{MM-dd-yyyy} - Whatever.log" />
Make sure you don't have any log commands before you set the LogName property.
Also check that logname is being set correctly ie, check that
ConfigurationManager.AppSettings("LogPath") & "Service." & Format(Now, "ddMMyyyy") & ".txt"
is not evalulating to null

WCF - One OperationContract sees Web.config, another doesn't

I have a WCF service with two methods, Ping and PutAddress. Ping works fine, but PutAddress was failing to initialize NHibernate correctly. Narrowing down the issue, I realized that the relevant settings from Web.config were not being read by PutAddress.
Strangely, Ping does have access to the settings from Web.config. I removed all of the NHibernate code and boiled it down to just trying to read the settings. Ping is able to read the settings (returns non-null, values in result are correct) while PutAddress' result is null.
Again, NHibernate is now completely out of the picture. Both methods simply try to read the relevant settings from Web.config. Ping succeeds while PutAddress fails.
Any thoughts?
Interface:
[ServiceContract]
public interface IMyService
{
[OperationContract]
string Ping();
[OperationContract]
Address PutAddress(Address address);
}
Implementation:
public class MyService : IMyService
{
public string Ping()
{
NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration result =
(NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration)
System.Configuration.ConfigurationManager.GetSection("hibernate-configuration");
if (result == null)
{
System.Diagnostics.Debugger.Break(); // Does NOT break, "Pong" returned
}
return "Pong";
}
public Address PutAddress(Address address)
{
NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration result =
(NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration)
System.Configuration.ConfigurationManager.GetSection("hibernate-configuration");
if (result == null)
{
System.Diagnostics.Debugger.Break(); // Breaks, result is null
}
return address; // Return version potentially modified with DB-assigned ID
}
}
EDIT:
Here is the (sanitized) Web.config
<?xml version="1.0"?>
<configuration>
<configSections>
<!-- NHibernate Section -->
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
<!-- Log4Net Section -->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<!-- NHibernate Configuration -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">
NHibernate.Dialect.MySQLDialect
</property>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="connection.driver_class">
NHibernate.Driver.MySqlDataDriver
</property>
<property name="connection.connection_string">
Server=localhost;Database=DB;User ID=USER;Password=PASS
</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name="show_sql">true</property>
<mapping assembly="MyService"/>
</session-factory>
</hibernate-configuration>
<!-- Log4Net Configuration -->
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender, log4net">
<layout type="log4net.Layout.PatternLayout, log4net">
<param name="ConversionPattern" value="%d %p %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="DatePattern" value="yyyy.MM.dd"/>
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%d %p %m%n"/>
</layout>
</appender>
<root>
<priority value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
</root>
<logger name="NHibernate" additivity="false">
<level value="WARN"/>
<appender-ref ref="RollingFile"/>
<appender-ref ref="ConsoleAppender"/>
</logger>
<logger name="NHibernate.SQL" additivity="false">
<level value="ALL"/>
<appender-ref ref="RollingFile"/>
<appender-ref ref="ConsoleAppender"/>
</logger>
</log4net>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Found the problem - one call was from a Unit Test that I hand-coded, which uses MyServiceClient that was generated by Visual Studio when I added a Service Reference. The other call was from a unit test that was created by the Visual Studio (2010) Unit Test wizard, which just instantiated MyService as an object. The VS-generated unit test was running in-process as a result.
I had an odd problem myself. I could not return "DataTable" objects from a WCF operation. I wonder if there could be a problem with returning an "Address" object from a WCF operation as well.
I don't know the complete answer to your question, but I would suggest the following
In your code for PutAddress, try replacing Address PutAddress(Address address); with string PutAddress(Address address); and replace return address; with return "Pong"
By doing so, you will ascertain the flow of execution through your code. Make sure that you are actually breaking and returning null as opposed to returning a null value of address.
If the new code returns null, then you are in fact having trouble reading from the file. I suspect that it will actually return "Pong", indicating that your code is returning Address, which somehow contains null because of WCF weirdness.