0

I'm trying to implement tracing in my webservice, but I'm getting the error:

Couldn't find type for class Host.AzureLocalStorageTraceListener, MyProject.Host.

I've got a webservice which is hosted in azure but which doesn't follow the usual structure for azure webservices.

There is an azure project with a webrole which points to MyProject.Host. This is the start up project.

There is a project called MyProject.Host, which contains the web.config for the service and an azure webrole.

Then we have the MyProject.Service project, which contains the service interface, IService1, and the service code, Service1.cs.

The code in web.config is:

    <sharedListeners>
       <add name="AzureLocalStorage" type="Host.AzureLocalStorageTraceListener, MyProject.Host"/>
    </sharedListeners>

And the error is:

Couldn't find type for class Host.AzureLocalStorageTraceListener, MyProject.Host. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Configuration.ConfigurationErrorsException: Couldn't find type for class Host.AzureLocalStorageTraceListener, MyProject.Host.

Source Error:

    Line 53:         private void RegisterRoutes()
    Line 54:         {
    Line 55:             RouteTable.Routes.Add(new ServiceRoute(string.Empty, new WebServiceHostFactory(), typeof(Service1)));
    Line 56:         }
    Line 57:     }

2 ideas I have have are that

  1. because the role and the service are in 2 different places, it's getting confused about where the AzureLocalStorageTraceListener is, or

  2. that because it hasn't been created by the usual Azure web service route that the AzureLocalStorageTraceListener hasn't been created.

But if it is these, I don't know how to fix them.

Does anyone have any ideas?

Thanks a lot!

rozza
  • 927
  • 2
  • 11
  • 24

1 Answers1

4

First you have to know that you'll need to create the AzureLocalStorageTraceListener yourself, this class isn't part of the Azure SDK:

using System;
using System.Diagnostics;
using System.IO;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace MyProject.Host
{
    public class AzureLocalStorageTraceListener : XmlWriterTraceListener
    {
        public AzureLocalStorageTraceListener()
            : base(Path.Combine(AzureLocalStorageTraceListener.GetLogDirectory().Path, "MyProject.svclog"))
        {
        }

        public static DirectoryConfiguration GetLogDirectory()
        {
            DirectoryConfiguration directory = new DirectoryConfiguration();
            directory.Container = "wad-tracefiles";
            directory.DirectoryQuotaInMB = 10;
            directory.Path = RoleEnvironment.GetLocalResource("CustomLogs").RootPath;
            return directory;
        }
    }
}

I suggest you add this to your MyProject.Host project, since this file relates to the host and not to the service. After adding this file, we know that:

  • Assembly: MyProject.Host
  • Class: MyProject.Host.AzureLocalStorageTraceListener (this is based on the namespace, not on the name of the assembly)

And you'll be able to reference it correctly in the web.config:

<sharedListeners>
   <add name="AzureLocalStorage" type="MyProject.Host.AzureLocalStorageTraceListener, MyProject.Host"/>
</sharedListeners>

Note: The code in the AzureLocalStorageTraceListener class assumes you have a local resource called CustomLogs.

Sandrino Di Mattia
  • 24,739
  • 2
  • 60
  • 65