Customize output in hosted CLR (.NET)

Tomasz 1 Reputation point
2021-09-16T13:14:22.597+00:00

Hi, I'm hosting CLR in Golang program (but it doesn't matter where). Is the way to configure CLR to capture output from .NET assembly executed in my in-process CLR runtime straight into a file? I run .NET assembly in hosted CLR and I want to capture this output straight to the file (or some buffer).

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,119 questions
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,601 Reputation points
    2021-09-16T14:52:57.693+00:00

    You change the standard output for the application

    https://learn.microsoft.com/en-us/windows/console/setstdhandle

    0 comments No comments

  2. Tomasz 1 Reputation point
    2021-09-16T14:59:03.953+00:00

    Yes, I know but if I run two applications in this hosted CLR the output for the application will be mixed. I want to run many programs at the same time and I want to capture output for each specific application. Those applications (run in CLR) only print on standard console output using Console.WriteLine and so on. The format is just the text like Console.WriteLine("test...").

    0 comments No comments

  3. Bruce (SqlWork.com) 55,601 Reputation points
    2021-09-16T17:51:57.607+00:00

    Console.WriteLine uses the processes standard output file handle. so to capture all standard output for your process, you redirect standard output to new file stream with SetStdHandle() system call.

    There is only one standard output file handle, so all Console.WriteLine() call in the process go to the same file. you could also just start your golang program with standard output redirect.

    go run myprogram.go > somefile.log

    0 comments No comments

  4. Tomasz 1 Reputation point
    2021-09-16T22:01:39.027+00:00

    It is not the case which I have. I host CLR in golang and I want to run many .net assemblies in the same time. If I capture output using SetStdHandle(), I will have a log file with mixed output from that ran .net assemblies. Just to be clear, I create AppDomain and so on for each .net assembly. So, I want to achieve independent output for each assembly. I run myProgram1 and myProgram2 (.net assembly) in the same time in hosted CLR and I have to make sure that this output comes from myProgram1 and another output comes from myProgram2.

    I assume that when I implement hosting interfaces the one of the interface covers I/O but I don't know which one.

    0 comments No comments

  5. Bruce (SqlWork.com) 55,601 Reputation points
    2021-09-17T15:44:44.493+00:00

    the CLR preforms I/O by calling the native I/O services (via p-invoke). There is only one standard output (Console.Write()) per process. To do want you want you have 2 options:

    1) if you need Console.Write() to work, the only option is to create a separate process for each application. When your host creates the process it can define standard output to be any file handle it wants. This is the standard approach.

    2) Create a replacement Console library that the applications use. after loading the application into that app domain, create a file handle for that domain, and pass it the hosted application.

    0 comments No comments