Hi,
If you’re unable to use Application Insights directly within your plugin, here are alternative approaches to capture and analyze plugin logs outside of Dynamics 365.
Alternative Solutions for Capturing Plugin Logs
1. Log to a Custom Entity in Dataverse
Instead of sending logs to Application Insights, you can create a custom entity in Dataverse (e.g., Plugin Log) and write log records directly from your plugin.
How It Works:
Create a custom entity (Plugin Log) with fields like Message, Severity, Timestamp, Plugin Name, and Error Details.
Modify your plugin to create records in this entity whenever you need to log data.
Example Code:
public class LogToDataversePlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
tracingService.Trace("LogToDataversePlugin: Execution started.");
Entity logEntry = new Entity("new_pluginlog");
logEntry["new_message"] = "Execution started";
logEntry["new_pluginname"] = "LogToDataversePlugin";
logEntry["new_severity"] = "Information";
logEntry["new_timestamp"] = DateTime.UtcNow;
service.Create(logEntry);
}
catch (Exception ex)
{
tracingService.Trace("Error: {0}", ex.Message);
Entity errorLog = new Entity("new_pluginlog");
errorLog["new_message"] = ex.Message;
errorLog["new_pluginname"] = "LogToDataversePlugin";
errorLog["new_severity"] = "Error";
errorLog["new_timestamp"] = DateTime.UtcNow;
service.Create(errorLog);
throw;
}
}
}
2. Use Azure Service Bus for Logging
If you can’t use Application Insights but have access to Azure Service Bus, you can push log messages to a Service Bus queue, which can then be processed by an external service (like an Azure Function or Power Automate flow).
How It Works:
The plugin sends log messages to a Service Bus queue.
An external Azure Function or Power Automate flow reads and stores the logs in a central location like Azure Table Storage, a database, or Application Insights.
Example Code:
using Microsoft.Azure.ServiceBus;
using System.Text;
using System.Threading.Tasks;
public class LogToServiceBusPlugin : IPlugin
{
private const string ServiceBusConnectionString = "Endpoint=sb://yournamespace.servicebus.windows.net/;SharedAccessKeyName=YourKey;SharedAccessKey=YourValue";
private const string QueueName = "pluginlogs";
private static IQueueClient queueClient;
public void Execute(IServiceProvider serviceProvider)
{
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
tracingService.Trace("LogToServiceBusPlugin: Execution started.");
SendMessageToQueue("Plugin executed successfully", "Information").Wait();
}
catch (Exception ex)
{
tracingService.Trace("Error: {0}", ex.Message);
SendMessageToQueue(ex.Message, "Error").Wait();
throw;
}
}
private static async Task SendMessageToQueue(string message, string severity)
{
queueClient = new QueueClient(ServiceBusConnectionString, QueueName);
string logEntry = $"{{ \"message\": \"{message}\", \"severity\": \"{severity}\", \"timestamp\": \"{DateTime.UtcNow}\" }}";
Message busMessage = new Message(Encoding.UTF8.GetBytes(logEntry));
await queueClient.SendAsync(busMessage);
await queueClient.CloseAsync();
}
}
3. Send Logs to an External API or Database
If your organization has an existing logging service or database, your plugin can send log messages there using an HTTP request or direct database write.
Example Approaches:
REST API: Send logs to an external web service using HttpClient.
SQL Database: Use SqlConnection to write logs to a central database.
Logging Service: Integrate with tools like Splunk, Log Analytics, or Serilog.
Example: Sending Logs via HTTP Request
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
public class LogToExternalApiPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
tracingService.Trace("LogToExternalApiPlugin: Execution started.");
SendLogToApi("Execution started", "Information").Wait();
}
catch (Exception ex)
{
tracingService.Trace("Error: {0}", ex.Message);
SendLogToApi(ex.Message, "Error").Wait();
throw;
}
}
private static async Task SendLogToApi(string message, string severity)
{
using (HttpClient client = new HttpClient())
{
string apiUrl = "https://yourloggingapi.com/logs";
string jsonPayload = $"{{ \"message\": \"{message}\", \"severity\": \"{severity}\", \"timestamp\": \"{DateTime.UtcNow}\" }}";
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
await client.PostAsync(apiUrl, content);
}
}
}
If you don’t have access to Azure services, Dataverse logging is the easiest alternative. If you need real-time log processing, Service Bus or an external API would be better options.
Hope this helps! Let me know if you have any questions.
Best,
Nikhil Sarpatwari
If you found my comment helpful or if this resolves your issue, please acknowledge it by liking the comment or letting me know. Thanks for helping keep the community engaged!