Написать в лог Azure Функция - PullRequest
0 голосов
/ 06 февраля 2020

Это смелость кода, который у меня есть. Я хочу написать в журнал для устранения неполадок. Это не нравится в других методах. Я видел несколько примеров, где они обсуждали, как добиться этого, но никакого реального кода я не смог найти, чтобы попробовать.

Я пишу функцию Azure из портала.

Любая помощь, которая наиболее ценится.

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

   bool isGood = Verify(myKey, myInput;

   return code != null
        ? (ActionResult)new OkObjectResult($"Request Successful")
        : new BadRequestObjectResult("Bad Request");

} // End Main

public static bool Verify(String key, String input)

    {

        log.LogInformation("Write something here");

    return;

    }

    private static string CreateToken(string message, string secret)

    {
        log.LogInformation("Write something here");
        return;
    }

Ответы [ 2 ]

1 голос
/ 06 февраля 2020

вам нужно передать ILogger вашим методам, присвоить его переменной c в методе Run:

опция 1:

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

   bool isGood = Verify(myKey, myInput, log);
   var token = CreateToken("abc","def",log);

   return code != null
        ? (ActionResult)new OkObjectResult($"Request Successful")
        : new BadRequestObjectResult("Bad Request");

} // End Main

public static bool Verify(String key, String input, ILogger log)

    {

        log.LogInformation("Write something here");

    return true;

    }

    private static string CreateToken(string message, string secret, ILogger log)

    {
        log.LogInformation("Write something here");
        return "";
    }

опция 2:

public static class Function1
{
   private static ILogger _log = null;
   public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
   {
    _log = log;
    log.LogInformation("C# HTTP trigger function processed a request.");

   bool isGood = Verify(myKey, myInput;

   return code != null
        ? (ActionResult)new OkObjectResult($"Request Successful")
        : new BadRequestObjectResult("Bad Request");

} // End Main

public static bool Verify(String key, String input)

    {

        _log.LogInformation("Write something here");

    return true;

    }

    private static string CreateToken(string message, string secret)

    {
        _log.LogInformation("Write something here");
        return "";
    }
}
0 голосов
/ 06 февраля 2020

Создайте статистику c ILogger на уровне класса, назначьте его в один из вызванных функций Azure Functions и затем используйте его в других ваших классах.

public static class LogTest
{
    static ILogger _log;

    [FunctionName("LogTest")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        _log = log;
        log.LogInformation("C# HTTP trigger function processed a request.");

        LogIt("Log this");

        return (ActionResult)new OkObjectResult($"Done logging");
    }

    private static void LogIt(string s)
    {
        _log.LogInformation(s);
    }
}
...