Link to home
Start Free TrialLog in
Avatar of SoLost
SoLostFlag for New Zealand

asked on

Trace not working in ASP.NET Web Service

Hi,
I have written an ASP.Net Web Service in VS 2005 and I can not seem to get the Trace command to work i order to write debugging information.

I have gone back to the basics.

Created a new web site of type "Web Service"
This creates a default Web Service called Service with a web function "Hello World".

Added "Imports System.Diagnostics" to the top of the file

Before "return 'Hello World" add the lines :
Debug.WriteLine("Debug - Testing")
Trace.WriteLine("Trace - Testing")

My service.vb file now looks like this :


Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Diagnostics

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
     Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Debug.WriteLine("Debug - Testing")
        Trace.WriteLine("Trace - Testing")
        Return "Hello World"
    End Function

End Class


Hit "F5" to start debugging.
Select "Ok" to modify the web.config to enable debugging.
On the web page that pops up, Click on "HelloWorld"
Click on the "Invoke" button.

I don't see any output from the trace command, only the Debugging command.

Can someone please try this and let me know how to get Trace to work.

Thank you
Avatar of igor_alpha
igor_alpha

Hello SoLost,

You forgot to ad listener for Trace.
Look at writeline method of Trace class description:
Writes the value of the object's ToString method to the trace listeners in the Listeners collection.

Here is example of how to initializw Trace Listener:
' Creates the text file that the trace listener will write to.
Dim myTraceLog As New System.IO.FileStream("C:\myTraceLog.txt", _
   IO.FileMode.OpenOrCreate)
' Creates the new trace listener
Dim myListener As New TextWriterTraceListener(myTraceLog)

Trace.Listeners.Add(myListener) 'If you want your listener to receive all trace output

myListener.WriteLine( _
   "This output will not go to the Listeners collection") 'If you do not want your listener to receive trace output, do not add it to the Listeners collection. You can emit output through a listener independent of the Listeners collection by calling the listener's own output methods

Here is article in MSDN:
How to: Create and Initialize Trace Listeners
http://msdn2.microsoft.com/en-us/library/sk36c28t.aspx

Avatar of SoLost

ASKER

Thanks for that.
Writing output to a seperate file is what I am trying to achieve at the end of this but the problem I am having is that the Trace command isn't even being run.

If I step through the code it doesn't even run the Trace line.  It just steps over it.

How come Trace isn't logging to the console like Debug is?

If I create a seperate VB Project both Trace and Debug work fine
ASKER CERTIFIED SOLUTION
Avatar of igor_alpha
igor_alpha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SoLost

ASKER

Yes, I have :

<configuration>
<system.web>
    <trace enabled="true" localOnly="false" />
  <compilation debug="true" strict="false" explicit="true" />
</system.web>
</configuration>

But it still doesn't run the Trace command.

Can you please test the instructions that I gave at the start and see if you can get Trace to work.

Thanks
I suppose problem that you Tracing work on released applications, not on debugged.
Instead of System.Diagnostic.Trace object try to use Page.Trace object in debug time:
Page.Trace.Write("Trace - Testing")
try going to http://localhost/YourWebServiceApplication/trace.axd

basically, replace service.asmx with trace.axd

I hope this helps
Avatar of SoLost

ASKER

igor_alpha, I do not get Page.Trace.  When I type "Page." the only options I get are :
CreateHTMLTextWriterFromType
Equals
ReferenceEquals

web4net, what I am essentially trying to do is to make my own tracelistener for the web service so that it can write the logging from all of the web service sessions to a file.  The problem that I am having is that I can't even get the Trace.Writeline to run.  As far as I can tell I have turned everything on :(
Did you examine Trace output on released app version?
One of reasons, as I said, can be that System.Diagnostics.Trace doesn't work on debug time.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SoLost

ASKER

web4net, I realise that.  The tracelistener wasn't working because the Trace line wasn't even being run.  I was just trying to find out how to get Trace working without people focussing on the Tracelistener.

I have solved my problem.  Instead of creating an ASP.NET web service I have created a regular VB.NET web service.  Everything works fine now.

Thanks for all of your help guys!
Trace.Write (or .WriteLine) did not work for me, even with the above information (mainly trace enabled), and TraceListener is not an issue if you just want to use the default listener.  I was not observing any trace information in the axd.

Context.Trace.Write is what worked for me and I observed the trace output in the trace.axd file Trace Information section, where I was expecting to see it.
I got this working by using HttpContext.Current.Trace.Write().  Also had to enable trace in web.config and set localOnly to false.