Reputation: 1065
I've got an issue with my Log4Net usage in my application. This is my app.config file...
<configuration>
<log4net>
<root>
<level value="INFO"/>
<appender-ref ref="FileAppender" />
</root>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="mylogfile.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>
</log4net>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"
/>
rest of app.config under here...
This is at the top of my Program.cs
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
I also have this at the top of my form partial class (is this a duplicate of the above?)
private static readonly log4net.ILog log = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
And this code is on the form load (which does nothing)
log.Info("Initializing...");
log.Fatal("blah");
The logger creates a log file called "mylogfile.txt" but doesn't actually write anything into it.
I know I'm doing something wrong, but I can't spot what it is easily :( any help would be awesome.
Upvotes: 7
Views: 9465
Reputation: 41
In your config file make sure that immediateFlush is set to true.
<immediateFlush value="false"/>
Upvotes: 0
Reputation: 449
In the end to make it work I had to use the Extension of log4net.
Get the Nuget package:
Microsoft.Extensions.Logging.Log4Net.AspNetCore
Then add this to program.cs:
builder.Logging.AddLog4Net();
I left this code, because I need this in other processes like the backgroundworker:
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
Upvotes: 0
Reputation: 31
Even i have also faced the same problem, When i'm searching and searching, finally got an issue which was causing.
****First:**** Add below code in app.config file
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-3level %logger : %message%newline" />
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\\..\\Test_Output\\testLogFile.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-3level %logger : %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
****Second:**** You need to mention where you log4net located and Watch should be true
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)]
****Finally:**** You need to mention in your BaseClass or Class where you wanted to Log it: XmlConfigurator.Configure();
***Hope's it will Work.
Upvotes: 0
Reputation: 1904
I had the same issue.
For my fix, I had to remove
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
from my assemblyInfo.cs, if you're calling log4net.Config.XmlConfigurator.ConfigureAndWatch() in code.
Upvotes: 0
Reputation: 1065
I wound up figuring it out myself seconds later. The app.config was wrong (unsurprisingly, considering I wrote it).
Here is the working app.config startup.
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"
/>
<--- other section groups here, you will have these too --->
</sectionGroup>
</configSections>
<log4net>
<root>
<level value="INFO"/>
<appender-ref ref="FileAppender" />
</root>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="mylogfile.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>
</log4net>
I canned the
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
and replaced it with log4net.Config.XmlConfigurator.Configure(); (here it is, where it is called in the Main program startup in Program.cs).
static void Main()
{
log4net.Config.XmlConfigurator.Configure();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMOEBackup());
}
And it works! Thanks to everyone who viewed my question :)
Upvotes: 2
Reputation: 2566
http://log4net.sourceforge.net/release/1.2.0.30316/doc/manual/faq.html
From the documentation:
How do I enable log4net internal debugging?
• To enable log4net's internal debug programmatically you need to set the log4net.helpers.LogLog.InternalDebugging property to true. Obviously the sooner this is set the more debug will be produced.
• Internal debugging can also be enabled by setting a value in the application's configuration file (not the log4net configuration file, unless they log4net config is in the application's config file). The log4net.Internal.Debug application setting must be set to the value true. For example:
This setting is read immediatly on startup an will cause all internal debugging messages to be emmitted.
• To enable internal debugging from a configuration file, the debug attribute on the log4net configuration element can be set to the value true. For example:
... configuration ...
Using this method does require that your configuration file is located and loaded, otherwise the attribute will not be read. If you need to debug the process of locating the configuration file then use one of the other methods for enabling debugging.
Internal debugging messages are witten to the console and to the System.Diagnostics.Trace system. If the application does not have a console the messages logged there will be lost. Note that an application can redirect the console stream by setting the System.Console.Out. The Trace system will by default send the message to an attached debugger (where the messages will appear in the output window). If the process does not have a debugger attached then the messages are sent to the system debugger. A utility like DebugView from http://www.sysinternals.com may be used to capture these messages.
Upvotes: 0