Log4Net doesn't log when deployed - c#

I'm using log4net to log my application (This is a WPF application).
The logging works well in debug mode, but it doesn't work with my deployed version.
The application is installed in C:\Program Files (x86)\MyApp (I use InnoSetup to create the installer).
In debug mode, the log folder is well created and the log files too.
In the deployed version, nothing appears, the log folder isn't created.
Here is my log4net configuration:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log\main.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<maximumFileSize value="1000KB" />
<maxSizeRollBackups value="0" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
This is how I write in logs in my classes (this example is from App.xaml.cs):
protected static readonly ILog log = LogManager.GetLogger(typeof(App));
static App()
{
log4net.Config.XmlConfigurator.Configure();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
log.Info("This is an info log");
}
I've tried to change the level value to ALL, but nothing changes.
I think it may be a parameter to set, because it works well in debug mode (the logs files are properly created in the folder "x86\Debug\log\".
I've made some researches but I found nothing about that.

Sounds like a a permissions issue: Directory permissions (and UAC) will prevent your app from writing to anything below C:\Program Files (x86), unless you run it as admin. Change the log path to somewhere else, for instance ${LOCALAPPDATA}\MyApp\MyApp.log

Can you right click "C:\Program Files (x86)\MyApp" folder, Properties _> Securities and add the user you are running application with to Write privileges?
Or if you right click on you app and "Run as Administrator"?
Does it make difference?

Related

redirect log4net logs from third party

I have a third party using a configuration file that looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
<!--Others sections-->
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value=".\logs\logclient.txt" />
<appendToFile value="false" />
<rollingStyle value="Date" />
<maximumFileSize value="1000KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
The code in the third party looks like :
LogManager.GetRepository(Assembly.GetCallingAssembly()), configFile);
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
I would like the third party dll to use my own appender defined in my own configuration file. How can I managed this ?
NB :
the third party need to use its own configuration file because others
sections are mandatory and I can not add them in my file
I can modify the third party configuration file, I can not modify mine
There are two existing questions that propose a solution to dynamically edit log4net configuration:
Dynamically reconfigure Log4Net
How can I change the file location programmatically? (see JackAce answer).
As far as I understand, you must use the third party configuration file and you can modify it.
The configSource property/attribute should work to redirect the log4net configuration section of your third party configuration file.
In the third party configuration file :
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net configSource="pathtoyourlog4net.config" />
log4net library is a tool to help the programmer output log statements
to a variety of output targets.
You can also configure log4net in code instead of using a config file,this SO post clearly explains the question.
Change appender dynamically:
public static void ChangeFilePath(string appenderName, string newFilename)
{
log4net.Repository.ILoggerRepository repository = log4net.LogManager.GetRepository();
foreach (log4net.Appender.IAppender appender in repository.GetAppenders())
{
if (appender.Name.CompareTo(appenderName) == 0 && appender is log4net.Appender.FileAppender)
{
log4net.Appender.FileAppender fileAppender = (log4net.Appender.FileAppender)appender;
fileAppender.File = System.IO.Path.Combine(fileAppender.File, newFilename);
fileAppender.ActivateOptions();
}
}
}
Reference post
If your .NET app is referencing the thirdparty.dll, at runtime the app will ignore the thirdparty.dll.config completely. Also, the thirdparty.dll will not pickup the settings in the thirdparty.dll.config as it is expecting these to be loaded by the exe.
What you have to do is to merge (or replicate) the content of the thirdparty.dll.config into your own app.config. Both your app and the thirdparty.dll will be able to pickup the settings in your app.exe.config and you only need to maintain one .config file.
Furthermore, you can then add additional appenders to the log4net section and it will be picked-up by both your app and the thirdparty.dll.

Log4Net doesn’t write log in release mode - Console Application

I have a console application and have a class library which wraps the Log4Net methods. Now when run the application in debug mode it writes log but when it is built in release mode it doesn’t write log file. What would be the solution for this? The sample code and config file is given below
My development environment is
Visual Studio 2013 and .NET Framework 4.5
Console Application
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
log4net.GlobalContext.Properties["LogFileName"] = "TestLogin.txt";
Logger log = new Logger(typeof(Program));
log.Info("Logging is enabled!!");
}
}
}
App.config in Console Application
<?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.5" />
</startup>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value ="%property{LogFileName}"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level - %message%newline%exception" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
Class Library
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Logging
{
public class Logger
{
private readonly ILog log = null;
public Logger(Type type)
{
log = LogManager.GetLogger(type);
}
public void Info(object message)
{
log.Info(message);
}
}
}
I have followed the post and it didn’t help me to figure out why Log4Net doesn’t write in log file in release mode?
log4net doesn't log when running a .Net 4.0 Windows application built in Release mode
There are a few workarounds for this.
You could add the [MethodImpl(MethodImplOptions.NoInlining)]
attribute to your Logger constructor methods in your class library.
You could add [assembly: log4net.Config.XmlConfigurator(Watch =
true)] to AssemblyInfo.cs in your Console Project (not the class library project)
You can add log4net.Config.XmlConfigurator.Configure(); at the
start of your Logger constructor.
I think the reason this is happening is that in release mode, your class library is inlined and so when log4net attempts to find the attribute, it thinks the calling assembly is your exe which does not contain the attribute.
PS.
I presume you are aware that your Logger class will mean that you lose the ability to filter by method names, as log4net will only see the Logger.Info method name.
The line [assembly: log4net.Config.XmlConfigurator(Watch = true)] should be added to your AssemblyInfo.cs file in the Properties folder.
For users with an MVC app that already have the lines [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)] in AssemblyInfo.cs and log4net.Config.XmlConfigurator.Configure(); in Global.asax.cs, but it's still not writing, make sure that the Application Pool user has permissions to write to the location where you're writing the log.
To get around this, I simply created a log directory on the IIS server NOT inside inetpub, gave it adequate permissions (I suppose it can be "Everyone", "Full Control" if it's just a child log directory and you're feeling lazy), and wrote the full path in the log4net.config (or Web.config, if you didn't isolate it).
Here's my config file:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:/absolute/path/to/logfile.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maximumFileSize value="10MB"/>
<datePattern value="yyyyMMdd'-FULL.log'" />
<maxSizeRollBackups value="-1"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
Make sure that the log4net.config file is added in your final release binary and corresponds to the path mentioned in ConfigFile = "log4Net.config".
I had the same problem, then I realized that I was simply missing out this config file in my release binary.
I have this in my Logging.dll and my Program.exe assembly.cs. Then it works in both debug and release mode. My program is a windows service.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

Not able to get log4net working to write to file

I am using sharpdevelop to create a console application in C#. I have added in the reference for log4net and I added my logging statements while I was writing the code but I never looked at the log file. Now I am done with the code I need to get the log file working. My program runs fine, even the logging statements, but I can't find the log file.
I have tried to cobble together a couple of examples on getting log4net working. I have the lines to read from the configuration and then to instantiate the object, these are the first lines in my program to run:
log4net.Config.XmlConfigurator.Configure();
ILog datalogger = LogManager.GetLogger("myLog"); //initiate the data logger
Then in various places throughout the code I have this:
datalogger.Info(DateTime.Now.ToString() + ": using file: " + ProDirectory.ToString() + #"\" + myProFile.ToString());
I have also put the following in my app.config file:
<appender name="myLogAppender" type="log4net.Appender.RollingFileAppender" >
<file value="myLog.log" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level - %message%n" />
</layout>
</appender>
<logger name="myLog">
<level value="All"></level>
<appender-ref ref="myLogAppender" />
</logger>
No matter what I do, I can't see the log file being produced. I have changed the directory and even paused the program to see if I could find a handle open to the log file. Each time I come up empty. Not sure what I could be doing wrong.
not sure if this will help. I also had hard times making log4net work in different scenarios.
I use log4net root xml node to specify the appender. hope it will help.
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="250KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
UPDATE
You might also want to check Visual Studio output log. First setup the output log to log "ALL" (somewhere in VS settings). Then you should see exact error message of log4net in the output window of Visual Studio.
Whenever I have had issues with log4net creating the log file, it usually wound up being a file / directory permissions issue.
Change your <file value="log.txt" /> to something that you know will be accessible by the current user / process running devhost.
For example: <file value="%USERPROFILE%\Documents\log.txt" /> will create a log file in the user's My Documents folder. This is a folder that the user has permissions to write data to, and shouldn't give you any troubles with.
For more information on special folder values, see this link.
Looks like I just had to clean up my XML a little, after I did the log file is working fine:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections> <!-- Level 1 -->
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net"/> <!-- Level 2 -->
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<log4net>
<appender name="myLogAppender" type="log4net.Appender.RollingFileAppender" >
<file value="myLog.log" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level - %message%n" />
</layout>
</appender>
<logger name="myLog">
<level value="All"></level>
<appender-ref ref="myLogAppender" />
</logger>
</log4net>
</configuration>

Log4net doesn't write to file

I want add new log to file.this is my appender:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="mylogfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="test"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="All"/>
<appender-ref ref="RollingFileAppender"/>
</root>
and on my class I add
[assembly: XmlConfigurator(Watch = true)]
and I add access everyone for the file but: log4net doesn't write to file. Why?
Log4net fails silently when there's a problem. The design conceit is that no logging is preferable to taking down the application. To figure out what's wrong, turn on Log4net's internal debugging by adding this key to your [app/web].config file:
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
The debug messages will be written to the console or to the System.Diagnostics.Trace system. More details from Phill Haack at http://haacked.com/archive/2006/09/26/Log4Net_Troubleshooting.aspx/
There are any number of reasons Log4net might fail. Permissions problems on the log file directory, for starters (especially true for server processes, where your likely running under a restricted set of permissions for security).
You just need to call Configure:
log4net.Config.XmlConfigurator.Configure();
You can see more details here:
Log4net does not write the log file
You need to initialise the logging as the very first step in your app, and from the same assembly that you have the [assembly] tag:
From the docs:
Therefore if you use configuration attributes you must invoke log4net
to allow it to read the attributes. A simple call to
LogManager.GetLogger will cause the attributes on the calling assembly
to be read and processed. Therefore it is imperative to make a logging
call as early as possible during the application start-up, and
certainly before any external assemblies have been loaded and invoked.
Add something like this in your start up code:
LogManager.GetLogger("Initialises logging from assembly attributes");
You should add this config section:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
to refer the log4net configuration.

Log4Net Not Logging When Deployed

Using version 1.2.11. Logging works on my dev machine, but won't create the directory or log when deployed.
I tried:
giving full directory access to IUSR, Everyone, local users.
running the app pool as a local admin account.
to use internal debugging as Phil Haack describes here.
Stopped and started the app pool after each change.
Nothing is produced in the output file.
My log4Net config is below. Any thoughts on what to try next?
<?xml version="1.0"?>
<log4net debug="true">
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file value="..\Logging\log.txt" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<threshold value="DEBUG" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="console" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="file" />
<appender-ref ref="console" />
</root>
</log4net>
If the directory and the file is not being created, then most likely, the configuration is not being read (and therefore used) at runtime.
I always forget to add the single line of code for Log4net that hooks up the configuration. This code usually appears in the bootstrap class in the application (e.g. Global.asax for an ASP.NET app).
XmlConfigurator.Configure(new System.IO.FileInfo(configFile)); // configFile being the path to the file.
Instead of the above in-line, you can add this attribute to the AssemblyInfo.cs file:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Either way, this will wire up log4net. More information is found in the Manual Configuration section of the log4net docs.
If you're using IIS, make sure the correct group has modify access to the Logs folder (usually IIS_USERS).
Sounds like a permissions issue to me. I almost always use a directory where I don't have to enable any special permissions for the applications to write the log files to.
Here is what I generally use with log4net:
<file type="log4net.Util.PatternString" value="${ALLUSERSPROFILE}/<Company Name>/Logs/<Program Name>/<Log file name>.txt" />
Of cource you'll need to substitute Company Name, Program Name and Log file name in the above with actual values.
This will write to the ProgramData folder where access is typically not restricted. You can navigate to this folder in File Explorer by typing %ProgramData% or %AllUsersProfile%
Another thing I like about this method is that it works on nearly every microsoft O/S. XP, Vista, 7, 8
You probably do not know where you are logging:
<file value="..\Logging\log.txt" />
Will is derived from your running directory, which is iis its directory. You can better use a full path like:
<file value="c:\Logging\log.txt" />
Then you can give the right access rights to the logging directory. Log4net will not create any directories as far as I know. So you have to create the c:\logging directory first.
Non of the answers worked for me until I put these lines to the web.config app settings:
<add key="log4net.Config" value="Log.config" />
<add key="log4net.Config.Watch" value="True" />

Categories

Resources