Azure Functions v4 中的 Serilog 控制台主题

问题描述 投票:0回答:1
c# logging azure-functions serilog
1个回答
0
投票

根据与您的同一查询的Github评论,无法更改Azure Function Runtime附带的默认日志记录的颜色。此外,当您在 Visual Studio 或 Visual Studio Code 中触发函数时,函数是通过 Azure Function Core 工具命令行触发的,因此它采用后端 Azure Function SDK 中设置的默认日志记录。

我尝试实现 Serilog 并使用 Colorful.Console 包来更改响应的输出,但功能触发命令行终端无法读取它。

Colorful.Console:-

我的Function1.cs代码:-

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);
    }
}

输出:-

enter image description here

Serilog:-

我的Function1.cs代码:-

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;
    }
}

输出:-

enter image description here

代码中默认设置的 Azure CLI 颜色此处我也尝试了此Github问题中的建议,但即使我更改了代码中的控制台颜色,功能核心工具输出日志颜色也是默认的:-

我的 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;
        }
    }
}

输出:-

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.