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.
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:
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
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);
}
}
}
Right mouse click on the server project and add a new item (select Web\General\Global Application Class) called Global.asax
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);
});
}