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>
Related
I have a project in which I want to use log4net for logging. When I'm testing my project using a console app program, it does not log anything unless I write the log4net section in the app.config of the test project. How do i fix this ?
This is my configuration in my app.config file in the DLL:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="MyLogFile.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newline%exception" />
</layout>
</appender>
<root>
<level value ="DEBUG"/>
<appender-ref ref ="RollingFileAppender"/>
</root>
</log4net>
From log4net documentation page: https://logging.apache.org/log4net/release/manual/configuration.html (Configuration Files section)
The only way to configure an application using the System.Configuration APIs is to call the log4net.Config.XmlConfigurator.Configure() method or the log4net.Config.XmlConfigurator.Configure(ILoggerRepository) method.
Make sure you are initializing log4net on your console app.
I am using log4net for logging in my webapi. logging is working fine in my localhost or while i am deploying it on a app server. Same thing is not working in azure web service. Folder where log file has to be created is there but no files in there.
i have added below section in my web.config file,
<log4net>
<appender name ="ErrorLog" type ="log4net.Appender.RollingFileAppender">
<file value ="MyLogs\"/>
<staticLogFileName value ="false" />
<appendToFile value ="true"/>
<rollingStyle value ="Date"/>
<datePattern value ="yyyy-MM-dd.'Err'" />
<lockingModel type ="log4net.Appender.FileAppender+MinimalLock"/>
<layout type ="log4net.Layout.PatternLayout">
<conversionPattern value ="%d{DATE} [%t] %-5p %c - %m%n" />
</layout>
</appender>
<logger name ="ErrorLog">
<maximumFileSize value ="15MB" />
<appender-ref ref="ErrorLog"/>
</logger>
</log4net>
It seems that there are some mistake with log4net configuration.
Here I have a complete steps you could follow with and it works well on my site:
1.Install the log4net.dll binary using NuGet.
2.Configure the log4net name, type properties in the web.config
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
</configuration>
3.Configure the log4net properties
<log4net>
<root>
<level value="Debug"/>
<appender-ref ref="LogFileAppender"/>
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\Home\LogFiles\Log4Net\Log4Net.log"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="3MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
<logger name="SleepyCore">
<level value="INFO"/>
</logger>
</log4net>
Note: You need to specify the address of the log on azure like D:\Home
4.Modify the Global.asax Application_Start() method add the following code in the method.
log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
5.Write log with an instance of the ILog interface
ILog log = LogManager.GetLogger("SleepyCore");
log.Info("Begin - Page_Load() at " + DateTime.Now.ToString("hh.mm.ss.ffffff"));
6.The output as below:
For more details about how to configure log4net on azure, you could refer to this article.
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>
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 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.