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

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)]

Related

How to configure log4net with VS 2015 for an Integration Test?

I am working on an Integration Test for a REST Endpoint that connects to a vendors Rest Api on a hardware device. My software has to be tested on the physical device periodically. I created a test project (VSTest VS 2015) and I want to add logging to my test harness. I realize that there is a tremendous amount of documentation on log4net, but I still can't make it work. My goal is to log to the console and also to a file. The vendor wants a log file to validate that my tests completed.
First, I initialize log4net to read a standalone file.
using log4net.Config;
using log4net;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
namespace MyProgram
{
[TestClass]
public class AssemblyInitializer
{
private static ILog log = null;
[AssemblyInitialize]
public static void Configure(TestContext Context)
{
Debug.WriteLine("Starting log4net setup and configuration");
XmlConfigurator.Configure(new FileInfo("log4net.properties"));
log = LogManager.GetLogger(typeof(AssemblyInitializer));
log.Debug("log4net initialized for Integration Test");
Debug.WriteLine("Completed log4net setup and configuration");
}
}
}
My log4net.properties file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionpattern value="%date [%thread] %-5level %logger{1} - %message%newline"/>
</layout>
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="IntegrationTests.log" />
<appendToFile 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="ConsoleAppender"/>
<appender-ref ref="FileAppender"/>
</level>
</root>
</log4net>
</configuration>
I don't think the Test Explorer reads the Assembly.Info file, but I included a reference just in case.
[assembly: log4net.Config.XmlConfigurator(Watch = false)]
I have tried just about everything I can think of. I am out of ideas. Any help would be appreciated.
The code below works. It is not the same implementation, but I am getting both loggers to work. See my above comments. I believe that the log file was either in an unknown location, or I should have accessed a repository along the way. The following code works great and initializes before my tests are run, and logs my test results as needed.
using log4net;
using log4net.Config;
using log4net.Repository.Hierarchy;
using log4net.Core;
using log4net.Appender;
using log4net.Layout;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyProgram
{
[TestClass]
public class AssemblyInitializer
{
private static ILog log = null;
[AssemblyInitialize]
public static void Configure(TestContext Context)
{
Debug.WriteLine("Starting log4net setup and configuration");
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.RemoveAllAppenders();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();
FileAppender fileAppender = new FileAppender();
fileAppender.Name = "Integration Test FileAppender";
fileAppender.File = #"Logs\IntegrationTest.log";
fileAppender.AppendToFile = false;
fileAppender.Layout = patternLayout;
fileAppender.ActivateOptions();
hierarchy.Root.AddAppender(fileAppender);
ConsoleAppender consoleAppender = new ConsoleAppender();
consoleAppender.Name = "Integration Test ConsoleAppender";
consoleAppender.Target = Console.Out.ToString();
consoleAppender.ActivateOptions();
consoleAppender.Layout = patternLayout;
hierarchy.Root.AddAppender(consoleAppender);
hierarchy.Root.Level = Level.Debug;
hierarchy.Configured = true;
log = LogManager.GetLogger(typeof(AssemblyInitializer));
log.Debug("log4net initialized for Integration Test");
Debug.WriteLine("Completed log4net setup and configuration");
}
}
}
I'm going to expand on my comment. Because Log4Net drove me insane too.
This is all that's in my App.config in my Test project for NLog.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<appSettings>
</appSettings>
<nlog autoReload="true"
throwExceptions="true"
internalLogLevel="Debug" internalLogFile="c:\temp\nlog-test.log" >
<targets>
<target type="Console" name="console" error="true"/>
</targets>
<rules>
<logger name="*" writeTo="console" />
</rules>
</nlog>
</configuration>
You define a target which is a destination for logging and then specify which loggers write to that target.
They want you to use the LoggingManger to create a logger (a source) for every class you want to log from. But I just wrapped a single logger in a static class and created some pass-thru methods. You lose some details that the logger could automatically capture for you (e.g. the exact site of the logging call), but I usually have sufficent detail in the message I'm writing to log.
~
Nlog is on Github https://github.com/NLog/NLog. Log4Net is still svn. That tells me something.
There also some nice Windows forms targets for Nlog available on NuGet.

Log4Net Not Writing to Output File

I'm having serious trouble getting log4net to work with a Windows service (multi-project solution).
I first added reference to log4net.dll to the appropriate projects via NuGet. I then created a new Log4Net.config file in the root folder of the Windows service project. In the file's properties, I set Copy to Output Directory = Copy always. Below is the config file:
<?xml version="1.0"?>
<configuration>
<log4net>
<appender name="TestServiceLog" type="log4net.Appender.RollingFileAppender">
<file value="C:\Temp\Test.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<priority value="ALL" />
<appender-ref ref="TestServiceLog" />
</root>
</log4net>
</configuration>
In my Windows service project, in the AssemblyInfo.cs file, I added this line:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
In my main TestService.cs class, I referenced log4net and initialized the logger like so (omitted non-logging code):
public partial class TestService : ServiceBase
{
private ILog Log { get; set; }
protected override void OnStart(string[] args)
{
Log = LogManager.GetLogger(this.GetType());
Log.Info("Hear me log.");
}
}
I then installed and ran the service. I validated the service is running normally, but no log file is created/written to. No exceptions are being thrown. Everything appears in order when debugging. I turned on log4net's internal debugging and got this:
log4net: Creating repository for assembly [TestService, Version=3.6.5570.17497, Culture=neutral, PublicKeyToken=null]
log4net: Assembly [TestService, Version=3.6.5570.17497, Culture=neutral, PublicKeyToken=null] Loaded From [C:\ProgramData\Company\Applications\TestService\TestService\TestService.exe]
log4net: Assembly [TestService, Version=3.6.5570.17497, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: Assembly [TestService, Version=3.6.5570.17497, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: repository [log4net-default-repository] already exists, using repository type [log4net.Repository.Hierarchy.Hierarchy]
The output does not seem to hint at any issues. I've also tried adding the following line to my configuration, but it didn't do the trick:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/>
I added a WinForms project to the solution with the exact same configuration and logging worked fine, so I'm confident it's not my setup. Any other ideas on steps I may have missed?
I was able to get it to work by making the Log variable static and instantiating it like this:
private static ILog Log = LogManager.GetLogger(typeof(TestService));
I'm not quite sure why this was necessary. Hopefully someone else knows the answer.

Statement executed but logfile not created. What went wrong?

I followed logging instructions given here:
I created a Assembly.cs that has this the following:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
This is my web.config file:
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="FileAppender"
type="log4net.Appender.FileAppender">
<file value="C:\Users\SOIS\Documents\Visual Studio 2010\WebSites\DummyPharmacy\logfile.txt" />
<appendToFile 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="FileAppender" />
</root>
</log4net>
</configuration>
My connection class where I use logger has this:
using log4net;
public class Connection
{
private static readonly ILog log = LogManager.GetLogger(typeof(Connection));
The debug shows execution of the logging. No file created in my folder though. What went wrong?
My output file has this:
log4net:ERROR Failed to find configuration section 'log4net'
in the application's .config file.Check your .config file
for the <log4net> and <configSections> elements. The configuration
section should look like: <section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
I have made a separate log4net.config file. Because editing the existing one does not allow me to define a element.
This is because you didn't tell log4net to read config from your log4net.config file in your assembly attribute:
see the Configuration Attributes in official document:
// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.config in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.
Change it to following should work:
[assembly: log4net.Config.XmlConfigurator(ConfigFile="log4net.config",Watch=true)]
Or if you prefer a single .config file, then keep the attribute and move the configruations from log4net.config to Web.config.

Log4Net doesn't log when deployed

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?

log4net doesn't work for Windows Forms Application

I really wish someone could help me with that. It is really driving me crazy.
I have a simple simple Windows Forms application and I am trying to use the log4net library for logging (I am just testing it in this project because it didn't work out in my main project).
So I have the regular Form1.cs, app.config, AssemblyInfo.cs and Program.cs.
In my app.config I have:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="[Your Header text here]" />
<footer value="[Your Footer text here]" />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
In the Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using log4net.Config;
namespace log4net.test
{
public partial class Form1 : Form
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
log.Debug("This is a DEBUG level message. The most VERBOSE level.");
log.Info("Extended information, with higher importance than the Debug call");
log.Warn("An unexpected but recoverable situation occurred");
}
}
}
And in the end of the AssemblyInfo.cs file I have added:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)]
When I debug and go to the button1_Click I can see that nothing is happening. The log object has its:
IsInfoEnabled, IsDebugEnabled, IsErrorEnabled, IsWarnEnabled set to false and just nothing happens.
I've been trying to find a solution all day long and nothing. Can somebody help?
The problem is in this line:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)]
At runtime, there is no file "app.config" in the bin directory. You must either specify the correct file name like this:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "<your assembly name>.exe.config", Watch = true)]
or better just omit the name
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
I ran into a similar problem recently. If the logging does not work, I usually enable debug output as shown here: How to track down log4net problems
You need to set the root logging level in the configuration:
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
</root>
</log4net>
You will also need to set the configuration section for log4net. Setting the configuration section allows log4net to read settings from the app.config See Below:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
/// Add this section before the log4net node
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="[Your Header text here]" />
<footer value="[Your Footer text here]" />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

Categories

Resources