1

We can access all default trace messages in http://localhost/MyWeb/trace.axd

But how can I write my own custom error/logging messages to this trace. I tried

System.Diagnostics.Trace.Write()

But could not see my message in this trace screen.

Can anybody suggest how I can write and view my own trace messages.

I tried this in WCF_RIA Service which I called from LightSwitch's Silverlight client.

Chris Cook
  • 2,821
  • 1
  • 20
  • 32
M_Idrees
  • 2,080
  • 2
  • 23
  • 53

1 Answers1

2

We're currently using the following methods to write to the LightSwitch trace.axd log:

Microsoft.LightSwitch.Trace.TraceInformation("test info");
Microsoft.LightSwitch.Trace.TraceError(new Exception("test exception"));

The TraceError is useful as you can easily pass in a caught exception and it's highlighted in red within the trace.axd report.

We're also categorising our traces by supplying the optional category parameter as follows: -

Microsoft.LightSwitch.Trace.TraceInformation("CustomCategory", "test info");

In order to see these categorised traces you need to include the "CustomCategory" in the Microsoft.LightSwitch.Trace.Categories section of your web.config e.g.:

<!-- The semicolon-separated list of categories that will be enabled at the specified trace level -->
<add key="Microsoft.LightSwitch.Trace.Categories" value="Microsoft.LightSwitch;CustomCategory" />

As covered in the LightSwitch Team's Blog post Diagnosing Problems in a Deployed 3-Tier LightSwitch Application (Eric Erhardt), you'll need to consider the following settings in your web.config in order to enable the trace log:

  • Microsoft.LightSwitch.Trace.Enabled
  • Microsoft.LightSwitch.Trace.LocalOnly
  • Microsoft.LightSwitch.Trace.Level
  • Microsoft.LightSwitch.Trace.Sensitive
  • Microsoft.LightSwitch.Trace.Categories

Whilst this diagnostic sub system is intended for tracing the actions requested of the server and the server's response to each action, it can also be coerced into tracing client side operations by providing the client with a server side endpoint to the server side trace methods.

One option for providing this is to implement an ASP.NET Web API controller endpoint and calling this from the LightSwitch client. The following LightSwitch Team Blog posts provide an overview of implementing this type of endpoint:

Whilst these blog posts cover the general details, in this particular case, the following basic steps can be used:

Implement the WebAPI endpoint

  1. Right mouse click on the server project and add a folder called api
  2. Right mouse click on this new api folder and add a new item (select Web\Web API Controller Class) called TraceController.cs
  3. Code an endpoint in the TraceController along the following lines:

    public class TraceController : ApiController { [HttpGet] [Route("api/Trace/Information/{message}")] public void Information(string message) { using (var sac = ServerApplicationContext.CreateContext()) { Microsoft.LightSwitch.Trace.TraceInformation(message); } } }

  4. Right mouse click on the server project and add a new item (select Web\General\Global Application Class) called Global.asax

  5. Implement the following configuration in the Global.asax's Application_Start method:

    public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { GlobalConfiguration.Configure(config => { config.MapHttpAttributeRoutes(); }); } }

Quick example of calling the WebAPI endpoint from a LightSwitch HTML client

var message = encodeURIComponent("Hello JavaScript World");
var url = "../api/Trace/Information/" + message;
$.ajax({
    type: "GET",
    url: url,
    context: document.body
});

Quick example of calling the WebAPI endpoint from a LightSwitch Silverlight client

    Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(async () =>
    {
        System.Net.WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.BrowserHttp);
        System.Net.WebRequest.RegisterPrefix("https://", System.Net.Browser.WebRequestCreator.BrowserHttp);
        var message = "Hello Silverlight World";
        var uri = new Uri(System.Windows.Application.Current.Host.Source, "/api/Trace/Information/" + message);
        var request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
        request.BeginGetResponse(ac => { }, null);
    }); 
}
Chris Cook
  • 2,821
  • 1
  • 20
  • 32
  • I tried Microsoft.LightSwitch.Trace.TraceInformation("test info"); Microsoft.LightSwitch.Trace.TraceError(new Exception("test exception")); ,but did not see lines in trace.axd...do I need any additional settings for this? – M_Idrees Feb 06 '16 at 08:25
  • I have these settings in web.config: – M_Idrees Feb 06 '16 at 08:26
  • I write this code in LightSwitch screen's button event. – M_Idrees Feb 06 '16 at 08:26
  • @MIdrees I suspect this is the key to your issue, as the trace calls are primarily intended for server side debugging. Having said this, I'll update my answer to include details of an approach to utilise the trace options at the client level. – Chris Cook Feb 07 '16 at 23:41
  • thanks @Chris, good information. one problem I am facing with this code, I could not find ServerApplicationContext class, is it available in VS2012 or any alternative? – M_Idrees Feb 08 '16 at 07:31
  • I could not find ServerApplicationContext class, but it works without this. thanks @Chris Cook. – M_Idrees Feb 08 '16 at 07:54