I’m working on creating a custom logging solution that will insert my logs into an Azure Data Table. I also want to send those logs to Application Insights.
I’ve created a class called LogService and have added that to Dependency Injection so that it can be used by other classes like my DataManger class. In my LogService class, I’m setting the built in Logger that should be used for App Insights. The plan is that when my LogService.LogInformation function is called, I first log to App Insights and then I will send a Service Bus message off that will insert that log information into an Azure Data Table.
The log is getting added to the Azure Data Table but it’s not making it into App Insights. I’ve done some researching and I have my APPINSIGHTS_INTRUMENTATIONKEY
set and confirmed it is correct. I also have the APPLICATIONINSIGHTS_CONNECTIONSTRING
set in my Function Apps Environment Variables section. I also have the following NuGet package installed
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
Anyone know what I’m missing to get my logs inserted into App Insights?
Host.json
{
"version": "2.0",
"functionTimeout": "-1",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": false,
"excludedTypes": "Request"
}
},
"logLevel": {
"Function": "Debug"
}
}
}
Startup.cs
public override void Configure(IFunctionsHostBuilder builder)
{
//Add other dependencies here
builder.Services.AddTransient<ILogService, LogService>();
builder.Services.AddTransient<IDataManager, DataManager>();
builder.Services.AddTransient<IAuditLogManager, AuditLogManager>();
builder.Services.AddTransient<IRecordManager, RecordManager>();
builder.Services.AddTransient<IUtilities, Utilities>();
builder.Services.AddTransient<IEmailManager, EmailManager>();
var connstring = Environment.GetEnvironmentVariable("DbConnection");
builder.Services.AddDbContext<DataContext>(options =>
{
options.UseSqlServer(connstring,
sqlServerOptions => {
sqlServerOptions.CommandTimeout(180);
sqlServerOptions.EnableRetryOnFailure();
});
options.EnableSensitiveDataLogging();
});
}
LogService.cs
public class LogService : ILogService
{
private readonly IConfiguration _config;
private readonly ILogger<LogService> _log; //Use built in Logger service to send to App Insights.
public LogService(IConfiguration config, ILogger<LogService> log)
{
_config = config;
_log = log;
}
public void LogDebug(string message, string logType = "DEBUG")
{
LogInformation(message, logType);
}
public void LogError(string message, string logType = "ERROR")
{
LogInformation(message, logType);
}
public async void LogInformation(string message, string logType = "INFO")
{
//Log to built-in ILogger for App Insights and Mezmo
if (logType.ToUpper().Contains("ERROR"))
_log.LogError(message);
if (logType.ToUpper().Contains("DEBUG"))
_log.LogDebug(message);
else
_log.LogInformation(message);
//Get time in CST and convert to the RowKey format.
var cstDateTime = GetCurrentDateTimeCST();
var rowKey = cstDateTime.ToString("yyyyMMdd.HHmmssff");
string sbConnectionString = _config["LoggerURL"] ?? string.Empty;
string sbQueue = _config["LogServiceBusQueue"] ?? string.Empty;
var LoggingPartitionKey = _config["LoggingPartitionKey"] ?? string.Empty;
// Create a Log Message and send to Service Bus
var logEntity = new LogEntity();
logEntity.PartitionKey = LoggingPartitionKey;
logEntity.RowKey = rowKey;
logEntity.Message = message;
logEntity.LogType = logType;
//Serialize log object so be able to send in the message.
var messageBody = JsonSerializer.Serialize(logEntity);
try
{
ServiceBusClient serviceBusClient = new ServiceBusClient(sbConnectionString, new ServiceBusClientOptions()
{
TransportType = ServiceBusTransportType.AmqpWebSockets
});
ServiceBusSender serviceBusSender = serviceBusClient.CreateSender(sbQueue);
await serviceBusSender.SendMessageAsync(new ServiceBusMessage(messageBody));
}
catch (Exception)
{
throw;
}
}
public DateTime GetCurrentDateTimeCST()
{
DateTime cstDateTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"));
return cstDateTime;
}
}