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.
Related
I Have A C# Rest webService which working fine.
I needed to add logs using the log4net .
It's working in DEV/Staging environnements but not in production.
I Have granted the Application pool & all the user of machine the required rights on my logs folder but still cannot see the logs.
I'am using 4.5.2 framework ,Windows 2012 R2 as a hosting environement
My web.conf
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="F:\logs\WebService\ManageParty\%property{LogName}_%date{yyyy-MM-dd}.log" />
<rollingStyle value="Date" />
<datePattern value="yyyy-MM-dd" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger{1} %property{LogName} - %message%newline" />
</layout>
</appender>
<logger name="*****.WS.ManageParty" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
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>
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
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
I am using Log4net in my application.
Can anyone please explain how to read my log files(Txt) and show it in my application.because my log files are there in another server.
What will be the config settings to read the log files from the server and show it in the C# application.
This is the code have used to write the logs.
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="\\10.8.8.87\temp\logs.log" />
<appendToFile value="true" />
<maximumFileSize value="10MB" />
<maxSizeRollBackups value="50" />
<rollingStyle value="Size" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%identity---- %date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
Instead of an rollingfile appender you can better use for example a sql appender to log your exceptions to a database and from your application you can query the database. On this page you can find some config examples:
http://logging.apache.org/log4net/release/config-examples.html