Getting Started With Silverlight NuGet Package

If you have made it here chances are you have installed Unified Logging Silverlight NuGet package. If not, open up the NuGet package manager and search for “UnifiedLogging”.

Now that you have a reference to the client assembly you need to visit the Profile page and grab your Access Key and Secret Key.

To send data to Unified Logging you use the ULSubmitDataRequest like below.

var request = new ULSubmitDataRequest(accessKey, secretKey, submissionUrl, message);
request.SubmitAsync(OnCallback);

Then the callback method would be something like the below.

private void OnCallback(IAsyncResult result)
{
var submitResult = (SubmitResult) result;
if (submitResult.SubmitResultState.HttpStatus == HttpStatusCode.Created)
{
//Success!
}
else
{
//Failed!
}
}
 

Here is an example of a class called ULLog, which is contained in the code download sample, that will keep this functinality more contained, simple to use and leverage the failed request queue. To use the ULLog new it up when the application starts up with your Access Key, Secret Key and Submission Url.

/// <summary>
/// This is an example of how you can construct a logger for unified logging which uses the failover queue
/// </summary>
public class ULLog
{
private static string _accessKey;
private static string _secretKey;
private static string _submissionUrl;
private static ULLog instance;
private readonly FailedRequestQueue failedRequestQueue;
 
public ULLog(string accessKey, string secretKey, string submissionUrl)
{
_accessKey = accessKey;
_secretKey = secretKey;
_submissionUrl = submissionUrl;
failedRequestQueue = new FailedRequestQueue(ProcessFailedMessages);
 
instance = this;
}
 
public static ULLog Current
{
get
{
if (instance == null)
{
throw new Exception(“ULLog Not Initialized, please use constructor to initialize”);
}
 
return instance;
}
}
 
public FailedRequestQueue FailedRequestQueue
{
get { return failedRequestQueue; }
}
 
private static bool ProcessFailedMessages(string message)
{
try
{
var httpResult = HttpStatusCode.BadRequest;
using (var resetEvent = new ManualResetEvent(false))
{
var request = new ULSubmitDataRequest(_accessKey, _secretKey, _submissionUrl, message);
request.SubmitAsync(result =>
{
var submitResult = (SubmitResult) result;
httpResult = submitResult.SubmitResultState.HttpStatus;
resetEvent.Set();
});
 
resetEvent.WaitOne(TimeSpan.FromSeconds(30));
 
if (httpResult != HttpStatusCode.Created)
{
//failed to be submitted, stop processing and wait
return false;
}
else
{
//remove the message that was just sent
return true;
}
}
}
catch (Exception ex)
{
return false;
}
}
 
public void Write(string message, AsyncCallback callback)
{
var request = new ULSubmitDataRequest(_accessKey, _secretKey, _submissionUrl, message);
 
request.SubmitAsync(callback);
}
 
}

NEXT STEP: Learn About Indexes

Comments are closed.