user11081980
user11081980

Reputation: 3289

Log file not created in a .NET Core application running in a Docker container

I run a ASP.NET Core application in a Docker container and I am using Serilog.Extensions.Logging.File to write logs to a file.

I am able to see the logs when running the app locally (not in a container), but I am not able to see the logs when I bash into the container.

I am new to containerization and I was wondering if I am missing any steps.

Upvotes: 1

Views: 3658

Answers (2)

Imamul Karim Tonmoy
Imamul Karim Tonmoy

Reputation: 584

For Linux Host :

Change folder permission to 777 where you want to store logs.

chmod 777 .

docker run -d -t -i -p 80:80 -v /usr/Logs:/app/Logs --name testContainer testImage

Upvotes: 0

Edward
Edward

Reputation: 30056

Follow steps below to check the difference.

  1. Configure Serilog like

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureLogging((hostingContext, builder) =>
                {
                    builder.AddFile("Logs/myapp-{Date}.txt");
                })
                .UseStartup<Startup>();
    
  2. Build the image

    docker build -t dockersql .
    
  3. Run the image

    docker run -it -p 9999:80 dockersql
    
  4. Find the running container and exec the container

enter image description here

As the suggestion from @Chris, it is recommended to mount the volume, you could try like

    docker run -it -p 9999:80 -v D:\xx\TestDockerSQL\Test:/app/Logs dockersql

Upvotes: 3

Related Questions