My application uses:
.NET 4
MVC 3
Windows Server 2008 R2
I use log4net to write to a logs on event log.
My configuration file looks like this:
<configuration>
<log4net>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<param name="LogName" value="SomeLogName"/>
<param name="ApplicationName" value="MyAppName"/>
<some settings for log4net...>
</appender>
<log4net>
</configuration>
It works well when I run it on my computer (localhost).
But I can not write to event log on server.
You should check if the apppool user has full control over the logging directory. He has to have create and update rights.
Enable internal debugging:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
Or:
<configuration>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
...
</configuration>
The C:\tmp\ dir has to excits and you should give the apppool user full control on the directory.
Related
I have deployed a Asp.Net Core 2 app to IIS. The app is running fine but it keeps creating the following directory structure in the disk root:
D:\MYAPPNAME\MYAPPNAME\1.0.0
Even if i manually delete the folder, it gets created again when the app runs. What may cause this behaviour?
EDIT
After further investigation, i reproduced the issue using the VS 2017 template for asp-net core2 mvc application.
After adding Microsoft.Extensions.Logging.TraceSource nuget package and configuring the logs in app.config the unwated folder gets created...
This is the relevant part of the App.config
<system.diagnostics>
<sources>
<source name="TestApp" switchValue="All">
<listeners>
<clear />
<add name="RotatingFileLog" />
</listeners>
</source>
<source name="Microsoft" switchValue="All">
<listeners>
<clear />
<add name="RotatingFileLog" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="RotatingFileLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" initializeData="FileLogWriter" logFileCreationSchedule="Daily" traceOutputOptions="DateTime" BaseFileName="test" location="Custom" CustomLocation="E:\logs\test\" Delimiter="|" AutoFlush="True" Append="True"></add>
</sharedListeners>
<trace autoflush="true" indentsize="4">
<listeners>
<clear />
<add name="RotatingFileLog" />
</listeners>
</trace>
</system.diagnostics>
It has turned out that Microsoft.VisualBasic.Logging.FileLogTraceListener was creating the folder. I switched to another listener, in this case Essential.Diagnostics.RollingFileTraceListener
I have a web site project where I'd like to log via log4net. When I try it, log file is created but it is always empty.
Here is part of the Global.asax file:
void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/Log4Net.config")));
var log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Info("Logging started");
}
And here is Log4Net.config:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="INFO"/>
<appender-ref ref="FileAppender"/>
<appender-ref ref="ConsoleAppender" />
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="admin_log.log" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
</log4net>
Any ideas?
There is probably a problem in your configuration, enable internal debug to see what it is:
<configuration>
...
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Log4net FAQ
I had implemented the Windows Azure Auto Scale Application Block in console application which monitor the performance counters and add the instances to the web role. I implemented the logging also but it does not show me the logs in the console and it does not adding the new instances to the web role based on rules.
And it is not giving me any error...
Without knowing how the application is configured (in particular the content of app.config and of the schedule file), the best educated guess I can make is that your app.config is missing the configuration in <system.diagnostics> to enable the logging of messages from the block.
In short, in your app.config you should have a section similar to the following (taken from one of my sample applications):
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="Autoscaling General" switchName="SourceSwitch" switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="FileLog"/>
<remove name="Default" />
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
<source name="Autoscaling Updates" switchName="SourceSwitch" switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="FileLog"/>
<remove name="Default" />
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog"/>
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information"/>
<add name="SourceSwitch" value="Verbose, Information, Warning, Error, Critical"/>
</switches>
<sharedListeners>
<add name="FileLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" initializeData="FileLogWriter" location="ExecutableDirectory"/>
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
</sharedListeners>
</system.diagnostics>
You can find further information from MSDN.
In addition to his, I recall that I had to set some breakpoints (on Autoscaler.Start if I remind correctly) in order to catch some configuration errors.
Following the steps in this tutorial, the first item of "Setting up with IIS 7.5" after clicking on "Modules" in inetmgr, the following error occurs:
Full image: http://i.stack.imgur.com/QCM4s.png
Web.config in RavenDB
<configuration>
<appSettings>
<add key="Raven/DataDir" value="~\Data"/>
<add key="Raven/AnonymousAccess" value="Get"/>
</appSettings>
<system.webServer>
<handlers>
<add name="All" path="*" verb="*" type="Raven.Web.ForwardToRavenRespondersFactory, Raven.Web"/>
</handlers>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
</system.webServer>
<runtime>
<loadFromRemoteSources enabled="true"/>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="Analyzers"/>
</assemblyBinding>
</runtime>
</configuration>
applicationHost.config
http://pastebin.com/UJTJfB9f
Try
For a few attempts, I tried to change
this..
<section name="modules" allowDefinition="MachineToApplication" overrideModeDefault="Deny" />
to this..
<section name="modules" allowDefinition="MachineToApplication" overrideModeDefault="Allow" />
Results
When trying to access "in inetmgr Modules worked!"
However RavenDB Studio does not work.
The following image:
Config Error
This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false".
Config File
\\?\C:\Users\Riderman\RavenDB-Build-960\Web\web.config
Check your server web.config and change overrideModeDefault from Deny to Allow.
<configSections>
<sectionGroup name="system.webServer">
<section name="handlers" overrideModeDefault="Deny" />
<section name="modules" allowDefinition="MachineToApplication" overrideModeDefault="Deny" />
You can also manage sections on web server level (just select the Server in the left pane) in your IIS management console and then select "Feature Delegation":
As you see in the picture above all the features are Read/Write. Currently on my machine the Modules feature is Read Only, so I'd need to change it to Read/Write - in the right hand pane in Set Feature Delegation just click on Read/Write...
For a Windows service written in C# 4, I have log4net configured and working fine in development. But not in production.
Here is my config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs/Log.xml"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="100MB"/>
<layout type="log4net.Layout.XmlLayout">
<param name="Prefix" value=""/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
</configuration>
Within my AssemblyInfo.cs I have:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
When I run the app locally, stuff is written to the log as expected. When I run it on the server, the log file is created at the correct path but it remains empty. Not a single character is written to the log.
Clearly my configuration is being picked up since the file is at the proper location with the proper extension.
As a workaround, I removed the [assembly: log4net.Config.XmlConfigurator(Watch = true)] line from the assembly and just did it in code on application start.
I just had the same issue and fixed that, [assembly: log4net.Config.XmlConfigurator(Watch = true)] was in ninjectwebcommon file instead of assembly info. After i moved that to assemblyinfo it start working
This is a silly question... but are you sure your service has started correctly and is doing stuff?
I'd add the console appender and maybe the eventlong appender too, and try running your service as a console app, if possible, to have a look?
I'd say, since the log file is being created, things look fine with log4net...?