dockerlogging.net-corevisual-studio-2019

Access a log file generated by .NET Core Console Application inside Docker


I'm completely new to the Docker world and no matter where ever I searched for it, it seems like there's no applicable solution. I know many questions asks about the same issue surrounding logging, yet I could not find a basic, detailed solution that will help me to apply it myself.

tldr;

I currently have a .NET Core 5.0 Console Application that creates a log file using log4net to a local directory. I want to transfer the application to run through a Windows-based docker container and to access the log file while/after it ran to see its results.

I saw the docker documentation | volumes yet could not understand how is it possible to define and later on access the log file.

Is this the way to do it? what should I define differently on my application?


Solution

  • Probably it's not about changing anything in your application, you just need to identify the directory where your logs are stored in the container and attach a volume or bind a mount to it on the host part. So, you can either:

    Path with volume

    Creating a docker volume.

    docker volume create logs-from-net-app
    

    Inspecting the volume to find out where you can find its contents.

    docker volume inspect logs-from-net-app
    

    Attaching it to the container.

    docker run --mount type=volume,source=logs-from-net-app,target=//c/path/to/logs "hello-world"
    

    Path with mounting bind

    Making a bind mount during docker run

    docker run --mount type=bind,source=C:\logs,target=//c/path/to/logs "hello-world"