-3

I have an ASP.NET Core 8.0 Web API with a project reference to .NET 8.0 console app. An endpoint in the API controller calls the underlying console app (by direct method invocation and not by ProcessStart) to run some services. All the logging in the console app is done using Console.WriteLine.

I am worried if there will be concurrency issues if the same end point from controller is invoked multiple times in parallel calling the console app.

Controller call:

[HttpPost("SomeService")]
public string SomeService([FromBody]  JsonObject jobParams)
{
    return someService.InvokeSomeService(jobParams.ToString());
}

Console app service:

 public class SomeService
 {
     public string InvokeSomeService(string json)
     {
         string originalOutput = Console.Out;

         using (StringWriter stringWriter = new StringWriter())
         {
             try
             {
                 string version = "1.0";
                 string logData = "";

                 Console.SetOut(stringWriter);

                 ServiceLogger.LogInformation($"***Start invoking the Some service {version}***");
                 Feeds feeds = new Feeds();
                 feeds.Start(json);
                 ServiceLogger.LogInformation($"***Completed running the Some service ***");

                 stringWriter.Flush();
                 logData = stringWriter.ToString();
                 feeds.SaveFeedsServiceLogs(logData);
                 return logData;
             }
             finally
             {
                 Console.SetOut(originalOutput);
             }
         }
     }
 }
8
  • 3
    What is the question? Yes, this is a problem, you really shouldn't use a Console App like that. At all. Extract the wanted functionality into a library project and use that. (And drop the Console.WriteLine()s) Commented Feb 17 at 11:29
  • 1
    What are you trying to do here - seems nonsensical for a WebApi to redirecting logging to console ? Even ignoring that, what is your writer configured to output to ? Use the built in asp.net ILogger Commented Feb 17 at 11:32
  • @fildor sorry if I was not clear, the app is actually a library project. The we api references this library to invoke some services Commented Feb 17 at 12:06
  • @Auburg the web api is not redirecting the logs. Web api having a c# library project invokes the project to run some services. The underlying library project is a legacy one and it has logs written using console.writeline. Earlier this exe was used to be triggered by another exe and that calling exe used to read all the console output logs. Now we have replaced the calling exe with a web api. Commented Feb 17 at 12:12
  • 1
    I'd strongly suggest a rewrite, then. With proper input/output, proper logging using the canonical Logging APIs and testing and all you'd want in a modern library. Commented Feb 17 at 13:13

1 Answer 1

0

You can use built-in logger instead creating logger yourself: https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#get-started (Logging in C# and .NET microsoft article)

Add logger in your startup file (pay attention to the .AddConsole() call):

services.AddLogging(builder => builder.AddConsole());

And then use it:

public class YourClass(ILogger<YourClass> logger) {
    [HttpPost("SomeService")]
    public string SomeService([FromBody] JsonObject jobParams)
    {
        logger.LogInformation(...);
        return someService.InvokeSomeService(jobParams.ToString());
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response but I am not asking for logs in the web api. I am asking about how we can read the logs from the underlying app called from web api.
I think you can write logs into file using simple self-coded solution or use another provider for logging: learn.microsoft.com/en-us/dotnet/core/extensions/… (serilog for example (global logger) - stackoverflow.com/questions/60588284/…) and read logs from another app
Or you just can log from main app and just pass logs to display by method parameters or event system :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.