Skip to content

Instantly share code, notes, and snippets.

@stefanolsen
Last active January 22, 2019 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanolsen/b8fdea6e0909af823e70a6da94777afb to your computer and use it in GitHub Desktop.
Save stefanolsen/b8fdea6e0909af823e70a6da94777afb to your computer and use it in GitHub Desktop.
Code piece for blog post about applying Azure Application Insights to InRiver iPMC extensions. Read about it here: https://stefanolsen.com/posts/applying-cloud-logging-to-inriver-ipmc-extensions-for-easier-analytics/
public class DummyEntityListener : IEntityListener
{
private LoggingScope _loggingScope;
public inRiverContext Context { get; set; }
public Dictionary<string, string> DefaultSettings => new Dictionary<string, string>
{
{ SettingsConstants.DisableTelemetry, "TRUE" },
{ SettingsConstants.InstrumentationKey, "REPLACE ME" }
};
public string Test()
{
_loggingScope = new LoggingScope("DummyEntityListener.Test", Context.Settings);
try
{
LongRunningOperation();
return "SUCCESS: Self-tests completed.";
}
catch (Exception ex)
{
// Catch and log unhandled exceptions.
Context.Log(LogLevel.Error, "Unhandled exception in Test method.", ex);
_loggingScope.TrackException(ex);
return "ERROR: Something failed.";
}
finally
{
// Flush all unsent entries form the buffer.
_loggingScope.Dispose();
}
}
private void LongRunningOperation()
{
// Log a regular trace message.
Context.Log(LogLevel.Information, "Starting a long running.");
_loggingScope.TrackEvent("Starting a long running.");
LoadEntities("Product");
LoadEntities("Item");
// Log a regular trace message.
Context.Log(LogLevel.Information, "Finished a long running.");
_loggingScope.TrackEvent("Finished a long running.");
}
private void LoadEntities(string entityType)
{
using (DependencyScope dependencyTracker =
_loggingScope.CreateDependencyTracker(
DependencyType.WcfService,
DependencyNames.DataService,
nameof(IDataService.GetAllEntitiesForEntityType)))
{
//ICollection<Entity> allProducts = Context.ExtensionManager.DataService
// .GetAllEntitiesForEntityType(
// entityType,
// LoadLevel.DataOnly);
//dependencyTracker.IsSuccessful = true;
// Simulate load time, instead of running above method call.
Thread.Sleep(1021);
// Set the 'IsSuccessful' property to 'true', so it appears properly in log views.
// Default value is 'false'.
dependencyTracker.IsSuccessful = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment