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);
}
}
}
}
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);
}
}
}
}
Share
Improve this question
edited Feb 17 at 11:35
marc_s
755k184 gold badges1.4k silver badges1.5k bronze badges
asked Feb 17 at 11:24
Prasanth SarathPrasanth Sarath
295 bronze badges
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) – Fildor 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 ILogger – auburg 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 – Prasanth Sarath 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. – Prasanth Sarath 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. – Fildor Commented Feb 17 at 13:13
1 Answer
Reset to default 0You can use built-in logger instead creating logger yourself: https://learn.microsoft/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());
}
}