I am trying to use basic logging for a windows service.
I added the reference to log4net.
I added the following in AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
I added the following to my App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" requirePermission="false" />
</configSections>
<!-- Log4net Logging Setup -->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<file value="c:\\CGSD\\log\\logfile.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
</configuration>
I have the following code in my service:
log4net.Config.XmlConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
log.Debug("test");
The file c:\CGSD\log\logfile.txt is created but nothing is ever written to it.
I've been through the forums all day trying to track this one down, but if I overlooked an already posted solution I apologize.
Your filter level looks to be configured at <levelMin value="INFO" /> but you are testing a log.Debug message. Change your configuration to have <levelMin value="DEBUG" /> and try again. If that doesn't fix it, there may be other config problems also.
You require a minimum level INFO and are trying to log a message with level DEBUG. As you can see in the documentation (http://logging.apache.org/log4net/release/manual/introduction.html), the priority of DEBUG is lower than INFO so your message is filtered out.
Related
log file is not getting created,
using below call to get logger and config file.
is it required to give full path for file?
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" requirePermission="false"/>
</configSections>
<!-- Log4net Logging Setup -->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="50" />
<maximumFileSize value="50MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
Does your code call
log4net.Config.XmlConfigurator.Configure();
on startup?
The <root> section I use has priority, not level:
<root>
<priority value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
(Apparently, that's not the issue https://stackoverflow.com/a/24188507/21336)
after adding below line in AssemblyInfo.cs, could see log file getting created and logs coming.
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
I have a windows service application which is basically a solution created with VS2015 and contains 2 projects.
The first project is dealing with the service itself (start, stop, etc) while the second one is maintaining the process that will be executed due to launch of the service.
I am using the log4net to log messages into a file.
I wanted to created 2 separate log files for each project.
This has been configured into the service project and is up and running.
while it is not working for the second project.
So mainly my question is how to configure log4net to log into different files within one solution?
Configure log4net within the executing project only. If you want log messages from an specific assembly logged to a separate file make sure the root namespace for that assembly is unique and define two log4net appenders, each logging to a separate file and use logging filters to filter by namespace.
<!-- Will log only from the My.Process namespace -->
<appender name="ProcessLog" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="My.Process" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<file value="process.log" />
<!-- layout, etc -->
</appender>
<!-- will log everything except from the My.Process namespace -->
<appender name="ServiceLog" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="My.Process" />
<acceptOnMatch value="false" />
</filter>
<file value="service.log" />
<!-- layout, etc -->
</appender>
And don't forget to add a reference to both appenders in the root element.
Assuming your first and second project have different root namespaces (e.g. Project1.Service and Project2.MaintainProcess), you can use 2 appenders and the logger hierarchy to split the log files by project:
<log4net>
<appender name="Project1Logger" type="log4net.Appender.RollingFileAppender">
<file value="Project1Logger.log" />
</appender>
<appender name="Project2Logger" type="log4net.Appender.RollingFileAppender">
<file value="Project2Logger.log" />
</appender>
<root>
<level value="ALL" />
</root>
<logger name="{Project1RootNamespaceHere}">
<appender-ref ref="Project1Logger" />
</logger>
<logger name="{Project2RootNamespaceHere}">
<appender-ref ref="Project2Logger" />
</logger>
</log4net>
If you don't want to use separate appenders and logger hierarchy then see this answer for an alternative method using a custom pattern substitution to use a single appender. I'm not sure of the performance implications of that approach though.
Complete configuration (with a MinimalLock file appender):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<log4net>
<!-- ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
<appender name="RollingFileAppender_Info" type="log4net.Appender.RollingFileAppender">
<file value="Info.log" />
<encoding value="utf-8" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100MB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message %exception%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="WARN" />
</filter>
</appender>
<appender name="RollingFileAppender_Error" type="log4net.Appender.RollingFileAppender">
<file value="Error.log" />
<encoding value="utf-8" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100MB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message %exception%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender_Info" />
<appender-ref ref="RollingFileAppender_Error" />
</root>
</log4net>
</configuration>
Context:
I have a simple Web API project with a controller and an action.
In the header of each request, I add an authorization token.
Everything work as expected. I can request a token based on some credentials, and use it to make HTTP requests successfully.
Problem:
When I add the following in the web.config:
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="logfile.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date: %-5level – %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
I get the following error:
XMLHttpRequest cannot load http://localhost:4042/token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 500.
If I remove the configuration for log4net everything works as expected.
Note: I do not have any setting in web.config that adds headers in a request.
Startup.cs is the only point in the project were I specify:
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
Question: How does the log4net configuration relate to preflight requests?
(based on my limited knowledge about the ASP.NET framework I would say it is non-sense). Is there a subtlety that am I missing?
I looked into the webconfig of my project and there is a part from a working web.config
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
...
</log4net>
Maybe you missed configSection?
use an external setting file for avoiding the problem :
At startup, call:
XmlConfigurator.Configure();
In your Web.config, specify log4net.Config in appSettings:
<add key="log4net.Config" value="Log.config" />
In the Log.config file put your configuration :
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="logfile.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date: %-5level – %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
<log4net>
I have two programs: program1 and program2 (they are modules of biggest program and could be run simultaneously), which of them use log4net.dll. Log4net.dll is one and shared between both programs. Each program has its own log configuration. Configurations are only different by logging path.
First path:
"..\Logs\Program1\Log_%date{yyyyMMdd}.log"
Second path:
"..\Logs\Program2\Log_%date{yyyyMMdd}.log"
Here is an example of logging configuration:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="..\Logs\Program1\Log_%date{yyyyMMdd}.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger � %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
Everything works almost fine, but sometimes program2 writes log to program1 log file. It looks like log4net.dll cached program1 logging settings, and when program2 asked to log, framework uses the same settings. Please suggest, how can I resolve it?
In order for the two programs to write to the same file, you should define loggers for each in their config files.
So, instead of just defining a <root> logger, define a specific logger for Program1 in its config, and the same for Program2 in its config:
<!-- program1.config -->
<appender name="Program1RollingFileAppender"
type="log4net.Appender.RollingFileAppender"> …
<logger name="Program1">
<level value="INFO" />
<appender-ref ref="Program1RollingFileAppender" />
</logger>
<!-- program2.config -->
<appender name="Program2RollingFileAppender"
type="log4net.Appender.RollingFileAppender"> …
<logger name="Program2">
<level value="INFO" />
<appender-ref ref="Program2RollingFileAppender" />
</logger>
Then, in each program, instantiate a logger with the same name, and it will pick up the correct appender:
public class Program1
{
ILog log = LogManager.GetLogger("Program1"); …
public class Program2
{
ILog log = LogManager.GetLogger("Program2"); …
I'm trying to debug why I get this error:
Error occurred while sending a direct message or getting the response.
on this line:
consumer.Channel.Send(consumer.PrepareRequestUserAuthorization(authCallbakUrl, null, null));
So I added log4net, but it doesn't work for me.
My web config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- Others sections -->
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler" requirePermission="false" />
</configSections>
<!-- log4net is a 3rd party (free) logger library that dotnetopenid will use if present but does not require. -->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="RelyingParty.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date (GMT%date{%z}) [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="TracePageAppender" type="OpenIdRelyingPartyWebForms.Code.TracePageAppender, OpenIdRelyingPartyWebForms">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date (GMT%date{%z}) [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="INFO" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="TracePageAppender" />
</root>
<!-- Specify the level for some specific categories -->
<logger name="DotNetOpenAuth">
<level value="ALL" />
</logger>
</log4net>
Tried with and without this:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="log4net" />
<bindingRedirect newVersion="4.0.30319" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
I configure the log4net instance
log4net.Config.XmlConfigurator.Configure();
Your binding redirect suggests you're using log4net v4.0.30319, which doesn't exist. The latest version is 1.2.11, and the version DNOA is compiled against is 1.2.10. Once you fix the binding redirect I suspect it will work.
GlobalApplication.cs:
decorate it
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
To get a reference to logger from other places ex.:
ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);