Reputation: 22964
In most ASP .Net MVC web applications where I log with log4net, I have one static logger that is accessed from anywhere in the application.
How I normally do it in an MVC application, in the global.asax:
namespace MyApplication
{
public class MvcApplication : System.Web.HttpApplication
{
public static ILog logger = LogManager.GetLogger("Logger");
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
}
...
}
I now have a .Net web service that doesn't appear to have the same entry point as an MVC application (through the global.asax) and I have no idea where to instantiate my logger. Can anyone help?
Upvotes: 2
Views: 8053
Reputation: 101
You can add the Global.asax file manually & use it to start log4net
void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
However, note that Application_Error cannot be used for global exception handling. A SOAP extension would be required to intercept the outgoing message at the server and log errors at that point.
A Web application can be comprised of multiple Web services. However, the Application_Error event within the Global.asax Syntax file cannot be used for global exception handling. The HttpHandler for Web services consumes any exception that occurs while a Web service is executing and turns it into a SOAP fault before the Application_Error event is called. Build a SOAP extension to process Web service exceptions in a global exception handler. A SOAP extension can check for the existence of an exception in the ProcessMessage method. Within the ProcessMessage method, check the Exception property of the SoapMessage passed when the Stage property is set to AfterSerialize. For details on SOAP extensions, see SOAP Message Modification Using SOAP Extensions.
https://msdn.microsoft.com/en-us/library/ds492xtk%28v=vs.100%29.aspx
Upvotes: 0
Reputation: 1407
You can use the service public constructor to instantiate your logger. Be careful, though, that depending on the service behavior, PerCall or Single, the constructor will be called for every request or only the first time the service is instantiated.
Upvotes: 0
Reputation: 1027
You can use the global.asax
protected void Application_Start(Object sender, EventArgs e) {
log4net.Config.XmlConfigurator.Configure()
}
Upvotes: 3
Reputation: 48230
You can have your own logging gateway
http://netpl.blogspot.com/2010/09/easy-log4net-integration-into-net.html
An advantage of such approach is that internally logging depends on your own api so you can easily switch between logging frameworks without the need to modify dependant code.
Another thing is that webservices do also start with global.asax and you probably have missed something.
Upvotes: 0