admin管理员组

文章数量:1122846

I have a C# function app which runs a very long operation, and a pipeline which invokes it with the AzureFunction@1 task. The way to use this task is to start the function actual work in a new thread, immediately returning a response to the pipeline that the function has started working, which then start the pipeline countdown.

The first implementation of the function used a single thread and then everything worked as expected, at least log wise. After converting the function to do its job in a new thread (using Task.Run(()=> ... ), what happens is that at first I see some logs from the new thread in app insights (logged with an ILogger), but after 3 or 4 entries, it just stops and I have no idea if the function is running or not (The pipeline itself keeps running, waiting for a response).

Is there something needed to be changed with logging when writing in a new thread? Something else that you think can affect it?

EDIT: Seems like using Console.Writeline DOES work. Don't know if it helps identifying the issue

A pseudo code of my code to demonstrate what I'm doing:

public MyWorkingClass(ILogger log) { _logger = log; }

public string Run()
{
  //this is not blocking so the return would be called immediately 
  Task.Run(() =>
  {
    _logger.LogInformation("starting");
    ... do more stuff ..
  }
  return "Starting task";
}

I have a C# function app which runs a very long operation, and a pipeline which invokes it with the AzureFunction@1 task. The way to use this task is to start the function actual work in a new thread, immediately returning a response to the pipeline that the function has started working, which then start the pipeline countdown.

The first implementation of the function used a single thread and then everything worked as expected, at least log wise. After converting the function to do its job in a new thread (using Task.Run(()=> ... ), what happens is that at first I see some logs from the new thread in app insights (logged with an ILogger), but after 3 or 4 entries, it just stops and I have no idea if the function is running or not (The pipeline itself keeps running, waiting for a response).

Is there something needed to be changed with logging when writing in a new thread? Something else that you think can affect it?

EDIT: Seems like using Console.Writeline DOES work. Don't know if it helps identifying the issue

A pseudo code of my code to demonstrate what I'm doing:

public MyWorkingClass(ILogger log) { _logger = log; }

public string Run()
{
  //this is not blocking so the return would be called immediately 
  Task.Run(() =>
  {
    _logger.LogInformation("starting");
    ... do more stuff ..
  }
  return "Starting task";
}
Share Improve this question edited Dec 8, 2024 at 9:06 CodeMonkey asked Nov 21, 2024 at 9:06 CodeMonkeyCodeMonkey 12.4k37 gold badges127 silver badges241 bronze badges 11
  • I don't think the azure function hosting model supports this way of working. In my mind the runtime executes the function, returns a response and then the process is halted. I am not sure a newly created thread will survive the runtime execution. Have you check whether your thread runs all the way to the end? – Peter Bons Commented Nov 21, 2024 at 9:15
  • But even Microsoft documentation says I need to open a new thread in order to work with AzureFunction@1 - learn.microsoft.com/en-us/azure/devops/pipelines/tasks/… – CodeMonkey Commented Nov 21, 2024 at 9:20
  • I see. Didn't know. Question: are you running on a consumption plan and how long is very long? More than say 5 minutes? – Peter Bons Commented Nov 21, 2024 at 9:26
  • The plan is Premium v2 so it may even have an unbounded run in time. It can take even hours. For now I set the function timeout to 2 hours – CodeMonkey Commented Nov 21, 2024 at 9:29
  • Is the logger created inside the Task.Run or is it passed using dependency injection? Can you post the code? – Peter Bons Commented Nov 21, 2024 at 10:13
 |  Show 6 more comments

1 Answer 1

Reset to default 0

What I eventually did was creating my own ILogger inside the new thread, using the same configurations as in my host.json file using this method:

internal static ILogger GetILogger(string loggerName)
{
 var configuration = new ConfigurationBuilder()
                        .AddJsonFile("host.json", optional: true, reloadOnChange: true)
                        .Build();

  using var loggerFactory = LoggerFactory.Create(builder =>
  {            
      builder.AddConfiguration(configuration.GetSection("logging"));
      builder.AddConsole();
  });

  var logger = loggerFactory.CreateLogger(loggerName);
  return logger;
}

This method is call at the beginning of the Task.Run scope and the returned logger works perfectly, including logging to app insight.

本文标签: multithreadingAzure function app logs stop outputting to app insights inside new threadStack Overflow