Log4Net on Windows Server 2003 - windows

I've been working on a web application using Visual Studio 2010 on a Windows 7 OS.
I used Log4Net for logging on the Event Viewer and that worked out great, meaning that everything is being correctly logged on my Windows 7 Event Viewer.
After installing this same project on a Windows Server 2003 Machine, I've noticed that nothing get's logged...
I already added the ASPNET on the Administrators group of the Windows Server 2003 machine but still the problem persists...
On the AssemblyInfo.cs file I added:
[assembly: log4net.Config.XmlConfigurator()]
On Web.config file I added:
...
...
-->
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<param name="LogName" value="MyLog" />
<param name="ApplicationName" value="MyApplication" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
<!-- ApplicationKit category - the presentation UI -->
<logger name="MyLogger">
<level value="INFO" />
<appender-ref ref="FileAppender" />
<appender-ref ref="EventLogAppender" />
</logger>
And finally on the code behind:
...
log4net.Config.XmlConfigurator.Configure();
Ilog log = LogManager.GetLogger("MyLogger");
...
I already created the MyLog key value on:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
But still, nothing gets logged there.
I'm 100% percent sure this works perfectly on Windows 7.
Do I need to make some oyher special configuration on Windows Server 2003?
Thanks in advance

Well...
As it turned out I also had to give the NETWORK SERVICE full permissions to the following key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
Voila

Related

log4net and NUnit 3.2 and console output in VS

In my NUnit 2.6 tests I used to see log4net log messages in the output window of Visual Studio, in the Tests section. Since I switched to NUnit 3.2, they are no longer displayed, which is very inconvenient. I tried searching and the best "solution" I came up with was to dump everything into debug strings, which can be viewed either via DebugView utility from SysInternals or when I actually debug a test - then the messages are shown in Debug section. However, I would really like to see my log lines in the Visual Studio even when not debugging. Any ideas? Visual Studio 2015. This is my current log4net config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="OutputDebugStringAppender" />
<appender-ref ref="DebugAppender" />
</root>
</log4net>
</configuration>
NUnit V2 captured log4net output and translated it into an NUnit event. For 3.0, we decided this was out of scope for NUnit and left it to log4net to display things. When running under the NUnit console runner, this works fine but it turns out to be a bit of a limitation under the VS adapter.
I think it would be reasonable for us to supply either an appender or an engine extension you could use to get log4net output into the form of an NUnit output event. It's a matter of someone volunteering to write it. If you think this is important, you might file an issue on github.
For NUnit v3 you can do that with this line of code:
Console.SetOut(TestContext.Progress);
Log4Net configured to use ConsoleAppender.
For me the solution was to set the ConsoleAppender's target property to "Console.Out":
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender, log4net">
<target value="Console.Out"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd MMM yyyy HH:mm:ss} %level [%thread] %method - %message%n" />
</layout>
</appender>
Did not need to set out to TestContext.Progress.
This is with NUnit 3.11 and the R# testrunner.
PS. Noticed we're using different appenders. I'll leave this in here anyway since it's came up in my main SERP. :)

Why wont my log4net log entries show up in Chainsaw on Windows 7

I'm trying to get log4net to log via udp to chainsaw but its not working on windows 7. My config files are as follows:
<log4net debug="true">
<appender name="trace" type="log4net.Appender.TraceAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<remoteAddress value="127.0.0.1" />
<remotePort value="8085" />
<layout type="log4net.Layout.XmlLayoutSchemaLog4j">
<locationInfo value="true" />
</layout>
</appender>
<root>
<level value="TRACE" />
<appender-ref ref="trace" />
<appender-ref ref="UdpAppender" />
</root>
my chainsaw config file looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<plugin name="UDPReceiver" class="org.apache.log4j.net.UDPReceiver">
<param name="Port" value="8085" />
</plugin>
</log4j:configuration>
All of this is per the documentation found in: http://logging.apache.org/log4net/release/howto/chainsaw.html
Yet none of the logs show up.
Figured it out. Looks like log4net has issues with ipv6 and windows 7. To get around these issues you need to add an entry into your host file that looks like this:
127.0.0.2 localhosttwo
then your UpdAppender needs to reference that DNS entry as so:
<remoteAddress value="localhosttwo" />
127.0.0.2 is the ipv6 address for your localmachine and you need an explcit dns entry or else log4net will throw an error if you try and use the numerical address in the config file.
Make sure to flush your dns after you change the hostfile

How to read sql generated by NHibernate in Visual Studio

According to what I know, there are 3 ways to read the sql script generated by NHibernate:
1. log4net
2. sql profiler
3. show_sql = true
Here I just want to talk about 3rd one for it's said that I can read the sql in the output window in Visual Studio. But whatever I do, I can see nothing?!
Becasue some guy said the "show_sql = true" just means "Console.WriteLine()", so I post a question here.
I have to say I don't get what I want, so here I summarize my questions:
in the output window in an web application:
Can the result of "Console.WriteLine()" be shown?
Can "show_sql=true" make the sql script be shown?
If yes, how?
I don't think Visual Studio will show console output for a class library or website project. What I do is configure log4net to write NHibernate's SQL to a text file, then open the file in Visual Studio. With the right configuration, VS will show updates to the file by clicking in the window.
In your Web.config (or app.config), define the log4net section, have NHibernate format the SQL nicely, create a text file appender, and direct NHibernate messages there:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="format_sql">true</property>
</session-factory>
</hibernate-configuration>
<log4net>
<appender name="NHibernateLogFile" type="log4net.Appender.FileAppender">
<file value="../Logs/NHibernate.log" />
<appendToFile value="false" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{HH:mm:ss.fff}%m%n==========%n" />
</layout>
</appender>
<logger name="NHibernate" additivity="false">
<level value="WARN" />
<appender-ref ref="NHibernateLogFile" />
</logger>
<logger name="NHibernate.SQL" additivity="false">
<level value="DEBUG" />
<appender-ref ref="NHibernateLogFile" />
</logger>
</log4net>
</configuration>
Then open up NHibernate.Log in Visual Studio. Because of the MinimalLock above, Visual Studio can read the file at the same time log4net is writing to it. When you click in the window, VS will reload the file. Just be sure to turn this off when you deploy the web site or application.

newline statment does not works in Log4Net under Vista Guest account

I'm using Log4Net (1.2.10.0) for logging in my application.
It's working fine - till I run it on Vista SP2 under Guest user. The log file does not contains a newline characters - all log is a single line.
This is not happen when I run the application as regular or admin user.
Only in case of built-in guest account.
Any ideas?
The configuration is as following:
<appender name="clientRollingFile" type="log4net.Appender.RollingFileAppender">
<file value="C:\users\public\client.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger [%type{1}] [%ndc] - %message%newline" />
</layout>
</appender>
Thank
Try the following layout, which includes a newline in code and not in configuration. See if the problem still occurs.
<layout type="log4net.Layout.SimpleLayout">
This won't "fix" anything, but it may help us narrow down the issue.

log4net only logs when running in the Visual Studio debugger

I'm running Visual Studio 2008 - and have a problem with log4net logging (v1.2.10).
I have a small console test program with a single log statement.
I have log4net configured for RollingLogFileAppender and ConsoleAppender.
When I run the compiled exe from the command line, I see the correct creation of the log file in my runtime directory. The log file is created (or appended to, when it exists), but the only output is the [Header] and [Footer] as configured. There is no output to console.
However, when I run under the debugger, the log message appears both in the log file and on the console. Following is my log4net configuration:
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="quicktest-log-" />
<appendToFile value="true" />
<immediateFlush value="true" />
<datepattern value="yyyy-MM-dd" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="1048576" />
<rollingStyle value="Composite" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<header value="[Begin Quicktest program log]
" />
<footer value="[End Quicktest program log]
" />
<conversionPattern value="%date{HH:mm:ss} [%thread] %-5level %logger: %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
This theory could be a stretch, but have you ensured that the app.config has been copied to the folder where your executable is? App.config must be copied to where the output executable is, and you also must rename it to <executablename>.config, so if your executable is MyProgram.exe, config must be in MyProgram.exe.config.
It is working now, but the mystery remains. Apparently a bellyful of Chinese buffet is the only solution to a problem like this, for once I had one, the problem went away.
My test program was a single file with
class Test
{
static void Main (string[] args)
{
.
. // some logging attempted here.
.
}
}
When I had the problem originally, I was doing the logging within Main(). Afterwards, I created a method on the class Test, instantiated class Test in Main, and moved the logging to the method. This removed the problem:
class Test
{
static void Main (string[] args)
{
var p = new Test();
p.Go ();
}
public void Go ()
{
. // some logging here.
}
}
This is still inconclusive. I moved it back the way it was originally and it began working. So, I must conclude that the answer to this conundrum is: Do not attempt first-time log4net test programs without a bellyful of Chinese food.
I had the same issue, and I was doing clickonce deployment.
I found that i need to add log4net.xml in Project properties > Publish > ApplicationFiles and make this log4net.xml as datafile.

Resources