Logging is one of the core pillars of application development. .NET Core has fantastic support for rich contextual logging which spans across distributed system using its Activity API. Azure Application Insights SDK offers extensive logging out of the box without writing a single line of code. Just wire up the SDK and you are good to go. However there may be situations where you may want more fine grained control over the logging experience. Fortunately, its as simple as writing:
Activity activity = new Activity("Test Message");
var operation = telemetryClient.StartOperation<DependencyTelemetry>(activity);
This TelemetryClient is injected in your application when you wire up Application Insights.
services.AddApplicationInsightsTelemetry(options => options.InstrumentationKey = "YOUR_AI_KEY");
This works seamlessly till you hit the road block of Domain Driven Design and Entity Framework Core. In DDD, entities represent a real-world business object and performs equivalent operations. To use these objects with EF Core, they must have a parameter-less public constructor. So how will we inject our TelemetryClient?
Fortunately EF Core allows injecting DbContext in entity classes. So in the highly contrived example in the linked Github repository, you can create a public property for TelemetryClient in your DbContext class.
public class SchoolContext : DbContext
{
public TelemetryClient AppInsightsClient { get; }
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
public SchoolContext(TelemetryClient telemetryClient)
{
this.AppInsightsClient = telemetryClient;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=SchoolDB;Trusted_Connection=True;");
base.OnConfiguring(optionsBuilder);
}
}
Now this telemetry client can be accessed in your entity classes like this:
public class Student
{
private readonly TelemetryClient telemetryClient;
public int StudentId { get; set; }
public string Name { get; set; }
public Student()
{
}
public Student(TelemetryClient client)
{
this.telemetryClient = client;
}
private Student(SchoolContext context)
{
this.telemetryClient = context.AppInsightsClient;
}
//Snip for brevity
}
This allows the Entity classes to still have an instance of TelemetryClient while still preserving the principles of DDD and allowing direct consumers of your class to instantiate it without any issue.
Also with the magic of .NET Core Activity API, logs generated in a single request are grouped together in a nice visual representation in Application Insights without you having to do anything.
Application Insights Structured Logs |
Full Code is available here - https://github.com/mayankthebest/efcore-ddd-log-example
If you have a better way of doing this, dear reader, please let me know in the comments!
Comments
Post a Comment
As far as possible, please refrain from posting Anonymous comments. I would really love to know who is interested in my blog! Also check out the FAQs section for the comment policy followed on this site.