Reputation: 1288
Hi I am trying to use Log4Net in WCF IIS hosted service, but it doesn't log any data. Has anyone achieved logging in WCF service using Log4Net?
Upvotes: 6
Views: 16369
Reputation: 1
Iısusr permission Okay.
Give write and modify permission.
Assamble.cs
Add at the end:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Upvotes: 0
Reputation: 59
I had the same issue as this and it was because I was using the relative path for the custom config file. I had the following line of code used:
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
Through log4nets debug output, it was telling me it was looking for the log4net.config file in a root IIS directory, rather than anywhere in my application path.
So, change the line to something more direct to avoid this issue (if you also configure your custom config in this way):
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(@"G:\MyProject\log4net.config"));
Upvotes: 0
Reputation: 2926
Another thing to be careful of is when you have a custom WCF ServiceHostFactory
defined in a separate assembly to your Wcf endpoint (eg to keep DRY) then the assembly where the factory is defined is the the "root" assembly from the point of view of log4net and the XmlConfigurator
attribute needs to be declared in that assembly.
This FAQ http://logging.apache.org/log4net/release/faq.html#trouble-webapp-stops-logging and linked comments explain that log4net wants to initialize as soon as possible
Upvotes: 1
Reputation: 26992
I'm successfully using log4net on my project within a self-hosted WCF application.
Our steps to setup are fairly straightforward.
Add the following line to the above project's AssemblyInfo.cs file (allows a custom log4net config file to be specified, which log4net will "watch" for updates. Quick, but maybe a bit dirty..)
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Add log4net.config file to console project and copy it to the output directory (file properties: "Copy to Output Directory")
Declare the logger as private static member of the classes where you need logging:
private static readonly log4net.ILog Logger =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Log where required:
Logger.Info("Starting console service host");
This article pretty much covers it: http://haacked.com/archive/2005/03/07/ConfiguringLog4NetForWebApplications.aspx
Upvotes: 8
Reputation: 3477
I am using log4net in MSMQ activated WCF service hosted in IIS 7. I use Ninject for dependency injection and installed Ninject.Extensions.Wcf, Ninject.Extensions.Logging.log4net NuGet packages and added log4net configuration section to Web.config file.
I've seen people suggest adding log4net initialization code to Global.asax, but this doesn't work for MSMQ activated services as far as I can tell. However, Ninject installs WebActivator package that allows you to run initialization code when web application starts. Ninject places its own code into App_Start\NinjectWebCommon.cs. To configure log4net you need to add code to Start method. Mine looks like this:
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
// configure log4net
XmlConfigurator.Configure();
bootstrapper.Initialize(CreateKernel);
}
Upvotes: 0
Reputation: 17028
Using the assembly attribute to configure log4net will not work since log4net will not check the right directory for the config file. Maybe you could try to use some relative path when specifying the config file but it is easier to use one of the suggested solutions here.
Upvotes: 0