首页 > 解决方案 > 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.

标签: dockerasp.net-core

解决方案


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

推荐阅读