0

I need to understand, how to access the console generated by the application console, from one class in another project, and write it dynamically. tried several solutions and none worked.

Down here are the scripts I'm trying to figure out how to do.

This Program is inside a console application project

public static void Main ( string[] args )
{
    Console.WriteLine("hello");
    LogToConsole ( "Go" );
    Console.ReadLine (   );
}


public static void LogToConsole ( string message )
{
    Console.WriteLine( message );
}

This Log is inside an WEB API project

this project have a ref of console application

public static class Log
{
    public static void Access ( string information )
    {
        Program.LogToConsole ( information );
    }
}

This project, when the WEB API is accessed, it pass a message that will be passed to this Access method.

How can I write a line in this console that is open from this other project?

Community
  • 1
  • 1
Aru
  • 11
  • 1
  • 1
    Writing to Console window not that straight forward in Web Application. You can write messages to Visual Studio Output Window, [Read here](https://stackoverflow.com/questions/29549029/is-there-a-console-log-for-asp-net). You should use logging framework if you want to write any messages from the Web App. Logging frameworks such as [Log4net](https://www.c-sharpcorner.com/blogs/how-to-use-log4net-in-asp-net-core-application) and [Serilog](https://carlos.mendible.com/2019/01/14/updated-step-step-serilog-asp-net-core/) – Chetan Jun 29 '19 at 01:16
  • Hey thanks for answer, but there is no way to access it via other script? like an event or delegate, that is recursive for using? – Aru Jun 29 '19 at 01:24
  • You can see console window when you run console application because output method for Console application is set to Console. For Web Application, that's not the case. And when you refer console app in Web app and run the web app, console app is not actually not running. There is no way I can think of you can achieve this without using some other tools such as logging framework. – Chetan Jun 29 '19 at 01:29
  • 1
    I think you have a bit of misunderstanding here. You have 2 independent applications here. If you want 2 applications to talk to each other, you need to implement some sort of inter process communication. Referencing one project from another one is not enough to achieve this. – Sergey Krusch Jun 29 '19 at 01:33
  • What is the real problem that you are trying to solve here? Why do you need to redirect logs from the web app to the console app? – Sergey Krusch Jun 29 '19 at 01:34
  • Hey thanks both for the answers, Serilog is exactly what i need. And @SergeyKrusch i was trying to build a log response for a console to have a visual feedback of entry data in the web app. – Aru Jun 29 '19 at 01:45
  • "i was trying to build a log response for a console to have a visual feedback of entry data in the web app" Just write log messages to a file (using a library like Serilog is how people usually do it) and then open terminal and do `tail -f your-log-file-name` – Sergey Krusch Jun 29 '19 at 02:21
  • if you have a web app, why aren't you giving visual confirmation in the web app? – John Lord Jun 29 '19 at 06:22

0 Answers0