Reputation: 2214
I am using Azure functions v4 with .NET8.0
and Serilog with the following packages
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
and currently my output in console looks like this:
I am setting up logging in Program.cs
like this
var logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.ApplicationInsights(
TelemetryConfiguration.CreateDefault(),
TelemetryConverter.Events,
LogEventLevel.Information)
.MinimumLevel.Debug()
.WriteTo.Console(theme: SystemConsoleTheme.Literate)
.CreateLogger();
services.AddLogging(configure => configure.AddSerilog(logger, true));
and utilize it with ILogger
in the Azure function endpoint.
From ASP.NET core
I am used to console colors like this, where each data type is colored differently, like string
, int
, DateTime
, etc:
How can I get Serilog Console colors like in the second image?
Setting theme: SystemConsoleTheme.Literate
in setup does not seem to be sufficient.
Additional: Since I get the timestamp from Serilog ine ach log, how can I remove the long gray timestamp like in the first image.
Upvotes: 2
Views: 401
Reputation: 8195
According to this Github Comment on the same query as yours, Its not possible to change the colour of Default logging that comes with Azure Function Runtime. Also when you Trigger your Functions in your Visual Studio or Visual Studio Code, The Functions are triggered via Azure Function Core tools command line, Thus it takes the default logging set in the Azure Function SDK in the backend.
I tried implementing Serilog and using Colorful.Console package to change the output of the response, But it is not read by the Function trigger command line terminal.
With Colorful.Console:-
My Function1.cs code:-
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using System.Net;
using Console = Colorful.Console;
using Colorful;
using System.Drawing;
public class Function1
{
private readonly ILogger _logger;
public Function1(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<Function1>();
}
[Function("Function1")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
ColorfulConsoleLog("C# HTTP trigger function processed a request.");
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString("Welcome to Azure Functions!");
return response;
}
private void ColorfulConsoleLog(string message)
{
var styleSheet = new StyleSheet(Color.AntiqueWhite);
Console.WriteLineStyled(message, styleSheet);
}
}
Output:-
With Serilog:-
My Function1.cs code:-
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Compact;
using System.Net;
public class Function1
{
private readonly Microsoft.Extensions.Logging.ILogger _logger;
public Function1(ILoggerFactory loggerFactory)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <sgr color=\"1\">{Properties:j}</sgr>{NewLine}{Exception}")
.CreateLogger();
_logger = loggerFactory.CreateLogger<Function1>();
}
[Function("Function1")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString("Welcome to Azure Functions!");
return response;
}
}
Output:-
Azure CLI colours set by default in the code here I also tried the suggestions in this Github Issue but the Function Core Tools Output logs colour were default even when I changed the Console color in the code:-
My Function1.cs:-
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using static Colors.Net.StringStaticMethods;
using System.Net;
using System.Drawing;
using Colors.Net; // Import the required namespace
namespace FunctionApp82
{
public class Function1
{
private readonly ILogger _logger;
public Function1(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<Function1>();
}
[Function("Function1")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
ConsoleColor yellow = ConsoleColor.Yellow;
var logMessage = "C# HTTP trigger function processed a request.";
_logger.LogInformation((logMessage));
Colors.Net.ColoredConsole.WriteLine(logMessage);
Colorful.Console.WriteLine(logMessage, Color.DarkGreen);
ColoredConsole.WriteLine(White(logMessage + "\n"));
Colorful.Console.WriteLine(logMessage, true);
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString("Welcome to Azure Functions!");
return response;
}
}
}
Output:-
Upvotes: 1