Web.config configuration when using separate Project for logging Log4Net - asp.net

I have a Visual Studio solution with 2 proyects:
Web Application
Library project for logging
My Web Application project doesn't have Log4Net reference. The Logging Project is the one that has the reference to the Log4Net dll and it has a Manager Class where I have all the methods for logging.
Now, I want to know what do I have to do in the Web Application WEb.Config file, I saw on the internet that I have to add a Section:
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net"/>
But my concern is that my web project doesn't have the Log4Net dll reference. Is there I way I can do that without adding Log4Net dll to my web project?

Yes, you can add the section element for log4net without referencing log4net.dll in your web application project.
To be sure, I double checked this with a scratch web application project (WebApplication1) and class library (ClassLibrary1) targeting .NET 4.0 in Visual Studio 2013. ClassLibrary1 referenced log4net.dll, but WebApplication1 did not; it just referenced ClassLibrary1 and had a log4net section element & related log4net element in its Web.config: logging to a text file via log4net worked beautifully.

It seems you have forgotten to configure log4net. Best practice is to add a configure attribute in your assably.cs:
// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.config in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.
// Configure log4net using the .log4net file
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.log4net in the application base
// directory (i.e. the directory containing TestApp.ex
You can add the attribute in your log4net dll, there is no need for a reference to log4net in you web project.

Related

app_code find assemblyqualifiedname of class

I have been following Scott Gutheries Blog on how to auto start an ASP.Net application and have an issue with assembly names.
Firstly the web site that I have been following is:
http://weblogs.asp.net/scottgu/archive/2009/09/15/auto-start-asp-net-applications-vs-2010-and-net-4-0-series.aspx
I have added the following code to my applicationHost config file, and as you may have guessed it does not work because of the type definition.
<serviceAutoStartProviders>
<add name="PreWarmMyCache" type="MyWebSiteName.PreWarmCache, MyWebSiteName" />
</serviceAutoStartProviders>
I have hunted around for a solution and came across this neat code.
Dim _a as New MyApp.PreWarmCache()
_a.GetType().AssemblyQualifiedName
This produces the following result.
"MyApp.PreWarmCache, App_Code.<#########>, Version=0.0.0.0, Culture=neutral, PublickKeyToken=null"
My problem comes from the ######### in the above assembly name as it is unique each time it is run and therefore I cannot use it in the applicationHost file above.
Is there a way to get that value fixed, so the value becomes fixed and does not change?
If you want control over the assembly name and version number that is generated for the site you should use a Web Application Projects instead of a Web Site Projects in Visual Studio.
You could read about the two types of projects on msdn : Web Application Projects versus Web Site Projects in Visual Studio.
With an application project the assembly name doesn't change for each build and you can easily reference the assembly.
To migrate from one type of project to another you will find advices here : Converting a Web Site Project to a Web Application Project
Add a new Class Library project to your ASP.NET website solution and call it Startup.
Create a new class in this library called "ApplicationPreload" that implements IProcessHostPreloadClient.
Add your new Startup class library as a Reference in your ASP.NET Website
Compile your Solution which will add Startup.dll to your website's Bin directory
Add the following to your applicationHost.config file right under the </sites> section
<serviceAutoStartProviders>
<add name="ApplicationPreload" type="Startup.ApplicationPreload, Startup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /
</serviceAutoStartProviders>

Redefine application settings in web.config

There is an ASP.NET application. It is referenced to a .NET project which has web reference to an asmx web service (for example this project is MyApp.Utility).
The URL to web service is stored in the application settings. There is such section in app.config for MyApp.Utility project.
<applicationSettings>
<MyApp.Utility.My.MySettings>
<setting name="MyApp_Utility_ExternalServices_SomeService" serializeAs="String">
<value>http://localhost:17455/ExternalServices/SomeService.asmx</value>
</setting>
</MyApp.Utility.My.MySettings>
</applicationSettings>
I want to change this URL in web.config for my ASP.NET application. Can I redefine application settings of MyApp.Utility without recompiling the code?
yes, you have stuff in .config files exactly so that you don't have to recompile to change stuff.
Depending on what kind of app and setup you have you might have to restart the application in order for it to read in the new values, but no recompile is necessary.
however:
if the .config file is for a non-website project (web.config) it will be called app.config and be placed in the project root. This is not the file being read runtime, the file actually being used is called ProjectName.dll.config and will be in the /bin folder next to the ProjectName.dll, when you compile the code msbuild copies and renames the app.config file into this location.

Where is the Assembly.cs file in a VS2005 Website project?

I am trying to add some assembly info for log4net so that it can have a file watcher. It appears that the website project does not have this file. Does this mean that I am doomed to use the web.config file and that I cannot split out log4net's config and use a file wathcer?
This file is created for a Web Application project, and not for a web site project.
Comparing Web Site Projects and Web Application Projects

Where are Web Application Project Assembly references stored?

Where are assembly refernces stored for a web application?
In a Web Site, I see assembly tags written to the assembly node in the web.config when you add a reference. I am just curious as to how the Web Application Project makes the connection to get the correct local dll?
I manually add the reference and the application builds, but the dll is not imported into the BIN folder, and the assembly nodes are not created in the web config as they are in a Web Site. I do a solution search for the text 'assembly="SomeAssembly..." and no results are found.
I am just curious as I am trying to centralize updating assembly references as a 3rd party control vendor puts out hotfixes on a regular basis and we end up having to run around and update all the individual page refernces to the assembly. I was able to do this effectively in the Web Site project, but I am fairly new to Web Application Projects. Any advice or links would be appreciated. I guess I'm looking for tips on assembly & control reference management for ASP.NET Web Application Projects.
Like most Visual Studio projects, references are kept in the project.
There are two kinds of reference:
Project References are references to another project in the same solution. They look like this:
<ProjectReference Include="..\VBClassLibrary1\VBClassLibrary1.vbproj">
<Project>{045D7D9F-8E44-4C4B-95F8-620E86593C5B}</Project>
<Name>VBClassLibrary1</Name>
</ProjectReference>
File references are references to an arbitrary file on disk:
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
If you expand the References folder and click on a reference, then look in the Properties window, you'll see that both kinds of reference have a "Copy Local" property. For project references it defaults to true, for file references to false (though maybe that's only if the file is in the GAC). Changing the default adds:
<Private>False</Private>
Since the assembly was not imported to the BIN folder, and your application works, I assume that it is stored in the GAC (global assembly cache) and marked as "copy local=false" in the reference properties. You don't see the reference to the assembly in the web.config, since your code behind assembly - YourApp.dll (which is always created for web-applications), contains a standard assembly reference to that assembly. When you run your application it loads the assembly from the GAC.
Those "missing" dlls are probably in the Global Assembly Cache and are available to all .NET applications.
You can add control references to the pages/controls section of web.config, which will apply to all pages in the application.
I had the same issue, deleting .vs folder in the directory of the project solved the issue for me as it forced the recreation of .suo file which (though bite code) had reference to the web.config file in the wrong directory

VS2008 Web Deployment Project Section Replacement with Elmah

I'm using a web deployment project and want to do some section replacement with the emlah/errorMail section. I don't want to send emails in debug build mode.
I have created the custom section and put it in an errorMail.config. In the properties of web deploy project under replacements, I have elmah/errorMail=errorMail.config.
The error I'm getting is An error occurred creating the configuration section handler for elmah/errorMail: Could not load file or assembly 'Elmah' or one of its dependencies. The system cannot find the file specified.
I'm guessing this is happening because Elmah is not in the GAC or it's not one of my projects. The Elmah.dll file does copy to the output\bin folder just fine though.
Is there a way to have section replacements work with 3rd party dlls?
[EDIT]
I found that if I drop the dll in question into the project folder for the web deployment project, that it will work fine. This is less than optimal.
So now my question is, how can I get this to work without having to put the dll in the GAC or having to copy the dll into the web deployment project folder?
If you add a reference to the elmah dll from your webproject, then it will automatically copy the dll to the output folder when you build it.
Those are the only 2 options though: to have the dll copied to the output directory or to put it in the GAC
A technique proposed here dynamically loads an assembly during the web deployment build process so that it doesn't have to be in the GAC. I tried, but was unable to get it to work, though.

Resources