【发布时间】:2020-08-03 11:39:02
【问题描述】:
我有一个需要在 Docker 容器中运行的 .NET Core api 项目。该项目使用 NLog 将数据记录到数据库中。我想在运行容器时在 nlog.config 文件中设置连接字符串属性。
应用程序将部署到不同的客户端,并将使用它们的数据库进行日志记录。
这就是我尝试在 nlog.config 文件中配置连接字符串的方式:
...
<target name="database" xsi:type="Database">
<connectionString>'${environment:logs_connection_string}'</connectionString>
<dbProvider>MySql.Data.MySqlClient.MySqlConnection, MySql.Data</dbProvider>
...
这是我的 Dockerfile:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Service/Service.csproj", "Service/"]
COPY ["Extensions/CustomExtensions.csproj", "Extensions/"]
RUN dotnet restore "Service/Service.csproj"
COPY . .
WORKDIR "/src/Service"
RUN dotnet build "Service.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Service.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Service.dll"]
ARG log_connection
ENV logs_connection_string=$log_connection
这些是我在运行时传递连接字符串的一些尝试:
- docker run -e log_connection="connection_string" -itd -p 1234:80 --name service service
- docker run -e log_connection_string="connection_string" -itd -p 1234:80 --name service service
我没有太多使用 Docker 的经验,所以这种方法甚至可能不可行。我愿意接受任何建议。
【问题讨论】:
-
NLog 在查找环境变量时应该没有任何问题。另见github.com/nlog/nlog/wiki/Environment-Layout-Renderer。您是否尝试过启用 NLog InternalLogger 并检查已解析的连接字符串。另见:github.com/NLog/NLog/wiki/Logging-troubleshooting
标签: docker asp.net-core nlog