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.
Related
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. :)
I'm trying to create environment specific configurations in Visual Studio 2010 and can't get the transformations to occur for anything other then the App.config. Here is an example of my scenario:
Added a configuration file in my project (e.g. Configs\Log4Net.config). The file is set to "Content" and "Copy Always"
<log4net>
<root>
<appender-ref ref="EventLogAppender" />
<level value="DEBUG" />
</root>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
...
</appender>
</log4net>
Added a transformation configuration file (e.g. Configs\Log4Net.Release.config)
<log4net xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<root>
<level value="WARN" xdt:Transform="SetAttributes" xdt:Locator="XPath(log4net/root/level[#value!='WARN'])" />
</root>
</log4net>
Modified the VS Project file to include the following target
<UsingTask TaskName="TransformXml"
AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterPublish" Condition="exists('Configs\Log4Net.$(Configuration).config')">
<TransformXml Source="Configs\Log4Net.config"
Destination="$(OutputPath)\Configs\Log4Net.config"
Transform="Configs\Log4Net.$(Configuration).config" />
</Target>
I've followed a similar pattern for the App.config and that works fine, but this one does not appear to work. My expectation is that when I do a Release build, the log level should be set to WARN.
I'm using XmlPreprocess tool for config files manipulation. It is using one mapping file for multiple environments. You can edit mapping file by Excel. It is very easy to use.
Try to replace your TransformXml task with Exec task and call XmlPreprocess tool.
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
I'm trying to add log4net logging to a web application I'm writing. I've got this in a web service method:
log4net.Config.XmlConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Methods));
log.Info("Some information");
And this in the web.config file:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Windows" />
<identity impersonate="true"/>
</system.web>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
I was expecting the log file to appear in the bin\ directory when I debug it through Visual Studio 2010, but nothing is written there. The method runs successfully without any exceptions, but I can't find the log file. Any ideas as to where it should appear?
Edit - Some extra info:
According to the notification area icon, the ASP.Net Development Server is running in:
C:\Users\jpope\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\
The permissions on this directory and the \bin\ sub-directory both give SYSTEM, me and Administrators full control, and no one else any access. The log file is not in the directory shown above or the \bin\ sub-directory.
If you're willing to wade into some low-level stuff, it might be interesting to see what Process Monitor thinks the web server process is trying to write to. At least it would be more fun than wading through the log4net documentation.
Maybe you should set the asp.net user has the write right for you web directory.
This turned out to be a configuration problem. Switching to a FileAppender instead of a RollFileAppender works fine. Now I need to find out what's wrong with the RollFileAppender configuration...
Edit: It's a RollingFileAppender, not a RollFileAppender.
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.