Hi All,

Any suggestions on how could we attach the NLog.config to a docker-compose.yml file so that it generates an “NLog_Internal.txt” on the host machine and we can see all the application logs in that file?
In other words, is there a way we could extract the logs of an application running inside a Docker container using NLog on a specified location (local disk or cloud)?

I have tried creating an ASP.NET Core application with the following NLog.config file:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      throwConfigExceptions="true"
      internalLogLevel="Trace"
      internalLogToConsole="true">

  <variable name="dockerlogDirectory" value="/data/logs/app"/>

  <targets>
    <target xsi:type="File" name="ownFile" fileName="${dockerlogDirectory}/MyCustomLog-${shortdate}.log"
            layout="${longdate}|${logger}|${uppercase:${level}}|${threadid}:${threadname}|${message} ${exception}" />
    <target xsi:type="Console" name="console" />
    <target xsi:type="Null" name="blackhole" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="console" />
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Info" writeTo="ownFile" />
  </rules>

</nlog>

Thanks.

Just to mention that my docker-compose.yml looks like the following:

version: '3.4'

networks:
  myapp-network:
    driver: bridge

services:
  myapp:
    image: myapp:latest
    depends_on:
      - "postgres_image"
    build:
      context: .
      dockerfile: Dockerfile
    env_file: .env
    ports:
      - "5000:80"
    volumes:
      - myapp_logsvolume: /data/logs/app
    environment:
      ISDOCKER: ${ISDOCKER}
    networks:
      - myapp-network    

  postgres_image:
    image: postgres:latest
    ports:
      - "5432:5432"
    restart: always
    volumes:
      - postgres_datavolume:/var/lib/postgresql/data
      - postgres_backupvolume:/backups
    environment:
      POSTGRES_USER: ${POSTGRESUSER}
      POSTGRES_PASSWORD: ${POSTGRESPWD}
      POSTGRES_DB: ${POSTGRESDB}
    networks:
      - myapp-network

volumes:
  myapp_logsvolume:
  postgres_datavolume:
  postgres_backupvolume:
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.