Log4Net and GAC - How to reference Configuration Files? - log4net

I am using log4net during my development, as as part of a project constraint, I now need to add it to the Global Assembly Cache.
The logging definitions are in a file Log4Net.xml. That file is referenced in my assemblyinfo as: [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.xml", Watch = true)]. So long as the xml file was in the same directory as the log4net.dll, everything has been working fine.
However now that I've added log4net to the GAC, it is no longer picking up the xml file.
Does anyone know what I need to change in order to have it pick up the XML file again? Is hardcoding the patch in the assembly reference the only way?
Many thanks

log4net expects the config file to be in the path returned by:
System.AppDomain.CurrentDomain.BaseDirectory
Let your application print this information to some file and then you know where you need to place the config file.
Of course there are other solutions, but then you cannot use the attribute anymore. Calling the ConfigureAndWatch() method directly allows you to figure out yourself where the config file is; you can even decide on a location (does not have to be a hard-coded path).

You might ensure that your log4net.xml file is set to "Copy Always" (Right Click on log4net.xml -> Properties -> Copy to Output Directory = Copy always). To ensure that your config file is being copied, you should check your bin\debug or bin\release directory and verify that the log4net.xml file exists in the same directory that your application executes.
If that doesn't workthen you can try enabling internal debugging in log4net. To enable internal debugging, add the following key to your app.config file. This will send internal log4net debug messages to your Visual Studio Output window (View -> Output).
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
For more information on log4net's internal debugging, you might check Phil Haack's blog post here.
If all else fails, you can turn on internal debugging and explicitly load the configuration by calling log4net's XmlConfigurator.ConfigureAndWatch method.
var fi = new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\log4net.xml");
XmlConfigurator.ConfigureAndWatch(fi);

Related

Do Elmah.config updates restart IIS?

This seems like a relatively simple question, but I didn't see it clearly indicated in the documentation I examined. Assuming a typical Elmah usage on a Windows Server 2012/IIS 7 webserver installation, if the Elmah web.config settings are pointed to an Elmah.config file, will altering that file trigger under the running application cause IIS to restart?
While the answer provided by Josh would normally be true, it is not in the case of ELMAH. ELMAH instantiates a new error log implementation every time it is needed, reading the configuration from either web.config or the configSource provided.
You can do a simple test to prove this. If you configure ELMAH to use an external config file for the errorLog element like this:
<elmah>
<errorLog configSource="elmah.config"/>
</elmah>
and your elmah.config looks like this:
<?xml version="1.0" encoding="utf-8"?>
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />
When you start the project and force an error, a new XML file is written to the App_Data folder. Change the logPath like this (without restarting the application):
<?xml version="1.0" encoding="utf-8"?>
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data2" />
(I changed to App_Data2).
Force a new error and you will now see ELMAH create the App_Data2 folder on the file system.
No: Straight from MSDN:
Saving a Web.config file restarts the application. You can alternatively use the configSource attribute of individual section elements to point to a secondary configuration file that does not cause an application restart when it is changed. For more information, see configSource in General Attributes Inherited by Section Elements.
MSDN

Enabling IIS Rewrite Module Logging

I am using IIS 7.5 on Windows 2008 Server R2 and have installed the IIS Rewrite Module v2.0. I am trying to enable logging to see exactly what rewrite rule is being used. Based on research I have learned that I need to modify (update) C:\Windows\System32\inetsrv\config\applicationHost.config. In order to do so I have created a NEW XML schema file called "RewriteLogging.xml" and have placed it in C:\Windows\System32\inetsrv\config\schema directory. The file contains the following code:
<!--
IIS Rewrite Logging
-->
<configSchema>
<sectionSchema name="system.applicationHost/RewriteLogging">
<attribute name="RewriteLog" type="string" defaultValue="C:\inetpub\logs\LogFiles\rewrite.txt" />
<attribute name="RewriteLogLevel" type="int" defaultValue="9" />
</sectionSchema>
</configSchema>
After restarting the web server I notice that the applicationHost.config file's date and time stamp get updated but my new directives are not getting added.
So here are my questions:
Am I approaching this the correct way?
Am I correct in creating a separate XML file and placing it in the schema directory?
Should I be creating a NEW section Schema name or should I be using an existing one? If so which one?
Am I using the correct syntax in the XML file? When I open it up in a browser I am not seeing any errors which is encouraging.
Am I missing something altogether or forgetting an important step?
Any help would be GREATLY appreciated.

log4net config settings

I am into the development of a core dll where I have a class library.I want to use log4net to enable logging for exceptions. I have an app.config file in the class library where i have given the settings for the log4net.However when I test the class library the log4net does'nt create logs until i add the app.config in the calling project inspite of the fact that i had added [assembly: log4net.Config.XmlConfigurator(Watch = true)] in the class libary's assemblyinfo.cs and I am using log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ErrorHandler)) where ErrorHandler is the name of my class library's class where log4net's calling functions are handled.Any ideas on what is going wrong?
Secondly, what I want to acheive is the users of my dll will just pass the location where they want to create logs and whether they want to create logs in event viewer or log files from their app.config? They will not handle any other setting of log4net.
Any suggestions or code snippets for the first issue and the second problem?
Only the "main" app.config is active for a .Net application. Your library config file is simply ignored. Either you transfer your settings to the main config file or you use an external config file for log4net. You configure it then for instance like this (assuming you call it log4net.config):
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Please note that the structure of the config file is a bit different:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="YourAppender" type="..." >
....
</appender>
<root>
<level value="ALL" />
<appender-ref ref="YourAppender" />
</root>
</log4net>
As for your second problem: I am not sure how flexible this has to be. Is it just switching from file appender to event log appender? Depending on your answer you may consider two prepare to configuration files (e.g. file.log4net and eventlog.log4net) and read the configuration as needed (in that case you cannot use the attribute: you call the Configure() method directly) or if your requirements are more complex you might even end up configuring log4net programatically.

How to setup NServiceBus and Log4Net to save to a file (without profiles)

I am running NServiceBus and having this issue. To try and troubleshoot it I would like to make NServiceBus log to a file.
The problem is that I am hosting NServiceBus in IIS, not NServiceBus.Host.exe. Because of that I don't know a way to turn on the the production profile.
Is there a way to tell NServiceBus to log to a file not using profiles? (Or a way to set the profile when not using NServiceBus.Host.exe?)
I had hoped that there is a way change NServiceBus.Configure.With().Log4Net(); to put an Appender in that will log to a file. But I don't see anyway to do that in code (all the examples use xml).
Alternatly if there is a way to change this to log to a file that would be great too:
<configSections>
<section name="Logging" type="NServiceBus.Config.Logging, NServiceBus.Core" />
</configSections>
<Logging Threshold="FINE" />
Does anyone know how to do either of these things?
Implement IWantCustomLogging:
https://github.com/NServiceBus/NServiceBus/blob/master/Samples/GenericHost/LoggingFromAppConfig/EndpointConfig.cs
And specify the RollingFileAppender in code:
Configure.Log4net< RollingFileAppender>(...)

Asp.net application error running with Sitecore 6.4.1

I have Sitecore as site root (which is running ok) and under it I need to have a bunch of asp.net applications running.
So far, created a virtual directory for the child application, turned it into an app. But as soon as I browse the child app it comes with this error message.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0246: The type or namespace name 'Sitecore' could not be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 3264: </providers>
Line 3265: </roleManager>
Line 3266: <profile defaultProvider="sql" enabled="true" inherits="Sitecore.Security.UserProfile, Sitecore.Kernel">
Line 3267: <providers>
Line 3268: <clear/>
It seems that the child asp.net app is inheriting Sitecore settings which are coming from Sitecore web.config.
Any ideas?
This should be possible. You'll have to author a web.config for that non-Sitecore application in virtual directory, and overwrite the sections under <system.web> and <system.webserver> which reference Sitecore classes. This includes:
httpModules / httpHandlers (for these you should "remove all" and add those of that web app)
security section (put the default ASP.NET provider classes back)
in Sitecore main web.config add the path of this web app to IgnoreUrlPrefixes setting to let Sitecore know it should not handle requests to those
It might be something else, but you should get the general idea.
This answer is similar to Yan, but is different enough for a separate answer. Hope it helps.
Go into IIS and select the Child Application. Select Modules. Remove all the Sitecore related modules that are present. Don't worry, the parent Sitecore app will still retain these modules.
When you do this, you are actually changing the child app web.config, so you will see the elements removed in the web.config file like so.
<remove name="SitecoreConfigWatcher" />
<remove name="SitecoreHttpModule" />
You also may have to clear out some other inherited settings.
<profile enabled="false"><providers><clear/></providers></profile>
<roleManager enabled="false"><providers><clear /></providers></roleManager>

Resources