How to get NHibernate to logging a file, not the console? - c#

If I turn on show_sql, then NHibernate logs its queries, which is what I do want to see. However, my app is a console program and turning on show_sql causes the SQL to appear in the console no matter what. I do not want to see it there in the console, but only in the files.
NOTE : I use log4net to control logging.
Any suggestions or guidance here on this issue are appreciated.

We can use log4net and some of its appenders (i.e. file appender like RollingFileAppender) to track NHibernate (including generated SQL)
There is a detailed how to:
Configure Log4Net for use with NHibernate
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\Logs\myLog.log" />
<layout type="log4net.Layout.PatternLayout">
<ConversionPattern value="[%-3d|%-4t|%-5p|%-75c] %m | %-10u %n" />
</layout>
<appendToFile value="true" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
</appender>
...
And then declare logger
<logger name="NHibernate">
<level value="WARN" />
<appender-ref ref="RollingFileAppender" />
</logger>
<logger name="NHibernate.SQL">
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</logger>
Note: here, we are not using configuration setting <property name="show_sql">false</property>. We just track with log4net

Related

Second Log4net logger not writing to unique file

I have an application that is using Anotar.Log4Net for logging. I cannot strip out the Anotar.Log4Net as other team members are using it. I would like to add my own logging file. I added an additional file appender to my app.config called BGDEV. I reviewed the post here to see how other's are accomplishing the task of having two file appenders. The desired behavior below is for each logger to log to it's own file. Everything is logging to MainLog.txt. Is this because there is something wrong with the app.config settings or because I have the anotar library installed?
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file value="MainLog.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<appender name="BGDEV" type="log4net.Appender.RollingFileAppender">
<file value="BG_Log.txt" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true"/>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="FileAppender"/>
</root>
<logger additivity="false" name="BGDev">
<level value="INFO"/>
<appender-ref ref="BGDev" />
</logger>
In code I create a logger and use it. Is there something wrong with my config file or is this becaise I am using
private static readonly ILog otherLog = LogManager.GetLogger("BGDEV");
otherLog.Info("TEST OTHER LOG");
You have a couple of issues with naming in your code and config. Names are case sensitive - you're defining a logger named BGDev, but you call GetLogger("BGDEV"), since no logger named BGDEV exists, it falls back to the root logger.
Your logger named BGDev also references an appender named BGDev, however again, no such appender exists, since you are defining it with an all-uppercase name BGDEV.
Finally, your BGDev logger needs to have a <layout>...</layout> section, or it won't be able to format log messages.
If you fix these issues, it should work as you expect.

C# Log4Net Writing To Both Appenders

I am using C# to write an in house application and we've been trying to figure out a resolution to this issue for a couple of days now. We're using Log4Net v1.2.15.0 compiling against .Net v4.5.2. I have two rolling file appenders, EventsLogger and SitrepLogger which are both set to write to the network. I have confirmed that this does work as intended however when I call one appender (doesn't matter which) to write out, it writes to both EventsLogger and SitrepLogger files. Here is the relevant data from my app.config
<log4net>
<appender name="EventsLogger" type="log4net.Appender.RollingFileAppender">
<file name="File" value="\\TS-WXLF41\Project\Ambushed\${USERNAME}\EventsLog" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="1MB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<param name="StaticLogFileName" value="false"/>
<param name="RollingStyle" value="Date"/>
<param name="DatePattern" value="_MM-dd-yy.\tx\t" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%newline%date{HH:mm:ss tt} %message" />
</layout>
</appender>
<appender name="SitrepLogger" type="log4net.Appender.RollingFileAppender">
<file name="File" value="\\TS-WXLF41\Project\Ambushed\${USERNAME}\logs\sitrep" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="1MB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<param name="StaticLogFileName" value="false"/>
<param name="RollingStyle" value="Date"/>
<param name="DatePattern" value="_MM-dd-yy.\tx\t"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%newline%date{HH:mm:ss tt} %message" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="EventsLogger" />
<appender-ref ref="SitrepLogger" />
</root>
</log4net>
In my Program's entry point constructor I call log4net.Config.XmlConfigurator.Configure(); which I am pretty sure is working as intended. I do not call Configure anywhere else in the code except from there. In the members are of my entry point class I call private static readonly log4net.ILog logger = log4net.LogManager.GetLogger("SitrepLogger"); to fetch the logger that I need for my Program (entry point) class. In my other classes I use the EventsLogger so I call private static readonly log4net.ILog eventslogger = log4net.LogManager.GetLogger("EventsLogger"); in the members are of those classes. I do not call GetLogger more than once per class and I am sure I invoking the correct appender by calling eventslogger.Info()
This used to work fine (using multiple appenders to write to different files) for us but we've made many many changes since then, which we have tried reverting to with no success. No errors, no warnings and no messages on compilation. Thank you for everything in advance! :)
You are confusing appenders and loggers. Your application is creating loggers "SitrepLogger" and "EventsLogger". These both inherit the configuration of the root logger, and write to appenders with the same names "SitrepLogger" and "EventsLogger".
Try the following configuration, which I think will give you what you want:
<log4net>
<appender name="EventsAppender" ... >
...
</appender>
<appender name="SitrepAppender" ... >
...
</appender>
<root>
<level value="ALL" />
</root>
<logger name="EventsLogger" additivity="False">
<appender-ref ref="EventsAppender" />
</logger>
<logger name="SitrepLogger" additivity="False">
<appender-ref ref="SitrepAppender" />
</logger>
</log4net>

Remove log4net system properties from output

I want to use LogicalThreadContext to pass some context information in my WCF service. I need to pass different properties. In C# I has code
LogicalThreadContext.Properties["MyProperty"] = 1;
In log4net config I have
<log4net>
<appender name="RollingLogFileAppenderSize" type="log4net.Appender.RollingFileAppender">
<file value="Logs\Log.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="3" />
<maximumFileSize value="5MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%2t] [%property] %level %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="RollingLogFileAppenderSize" />
</root>
</log4net>
And in log I got
2015-11-03 16:24:36,313 [10] [{MyProperty=1, log4net:Identity=, log4net:UserName=User, log4net:HostName=User}] INFO - Info
I don't want to have system properties log4net:Identity, log4net:UserName and log4net:HostName in log. How to do this? I can write config like this
conversionPattern value="%d [%2t] [%property{MyProperty}] %level %m%n"
But I have several properties in code and I want to see only properties that I added. Code
LogicalThreadContext.Properties.Remove("log4net:UserName");
doesn't work.
I found that it's posible to remove only log4net:HostName property with code
GlobalContext.Properties.Remove(LoggingEvent.HostNameProperty).
log4net:Identity and log4net:UserName cannot be removed because of CreateCompositeProperties method in log4net.Core.LoggingEvent class https://github.com/apache/log4net/blob/trunk/src/Core/LoggingEvent.cs.
It adds these properties without any conditions and so it's imposible to remove them for the last log4net version.
I had the same problem.
GlobalContext.Properties.Clear();
Worked for me.

How to get current username instead of AppPool identity in a logfile with Log4Net

We are using Log4Net from our ASP.NET MVC3 application, all works fine but we would like to see the current username instead of the application pool's identity in the log files, this is the appender configuration we are using:
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="ALL" />
<immediateFlush>true</immediateFlush>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<encoding value="utf-8" />
<file value="C:\Logs\MyLogs.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<maxSizeRollBackups value="30" />
<maximumFileSize value="25MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%property{log4net:HostName}] - %username%newline%utcdate - %-5level - %message%newline" />
</layout>
</appender>
<root>
<priority value="ALL" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
So it seems like the property: %username is retrieving the value of:
WindowsIdentity.GetCurrent().Name
Instead of what we would need: HttpContext.Current.User
Any idea on how we can solve this easily in the web.config without creating custom properties or additional log4net derived classes? If possible at all otherwise if custom property is the only way we can live with that I guess :) thanks!
Replacing %username by %identity should do it. It is working for me in my current project.
You can learn more about log4net with this excellent tutorial

log4net cant create log file , when publish to server

I am using log4net for logging my asp.net application. However the log file is working well when I run on my localhost. But when I publish to IIS, the log4net is not able to create a log file. May I know what is the problem? Did I miss any configuration?
Here is my log4net.config.
<log4net debug="true">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="D:\\LewreLogFile1.log"/>
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<logger name="File">
<level value="All" />
</logger>
<root>
<level value="All" />
<appender-ref ref="LogFileAppender" />
</root>
and my global.asax
void Application_Start(object sender, EventArgs e)
{
string l4net = Server.MapPath("~/log4Net.config");
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(l4net));
}
You have to give IIS permission to do IO. Change your App pool identity or give IUSR permission the the file system it's trying to access.

Categories