简体   繁体   English

NLog在Azure中不起作用

[英]NLog doesn't work in Azure

I'm trying to use NLog with an Azure Web App. 我正在尝试将NLog与Azure Web应用程序一起使用。 Everything works on my local PC, but when I deploy to Azure it doesn't work at all. 一切都可以在我的本地PC上运行,但是当我部署到Azure时根本无法运行。 I've tried with different targets like file and Azure table storage. 我尝试了不同的目标,例如文件和Azure表存储。 Both work perfectly on my PC but not in Azure. 两者都可以在我的PC上完美运行,但不能在Azure上运行。 I'm using asp.net core. 我正在使用asp.net核心。

Here is my NLog config for the table storage. 这是我用于表存储的NLog配置。
Note: this configuration works when I execute the program from my local PC. 注意:当我从本地PC执行程序时,此配置有效。 I can see the entries in the Azure table storage. 我可以在Azure表存储中看到条目。

<?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"
  >

  <extensions>
    <add assembly="NLog.Extensions.AzureTableStorage"/>
  </extensions>
  <targets>

    <target xsi:type="AzureTableStorage" name="allfile" 
            PartitionKey="nlog-all-${date}.${logger}"
            RowKey="${ticks}.${guid}"
            ConnectionString="**REMOVED**"
            tableName="**REMOVED**"
                 layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />

    <target xsi:type="AzureTableStorage" name="ownFile"
            PartitionKey="nlog-own-${date}.${logger}"
            RowKey="${ticks}.${guid}"
            ConnectionString="**REMOVED**"
            tableName="**REMOVED**"
              layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
    <target xsi:type="Null" name="blackhole" />
  </targets>

  <rules>

    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--Skip Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Trace" writeTo="ownFile" />
  </rules>
</nlog>

I noticed that the configuration file NLog.config was not getting updated (or is being overwritten) in Azure. 我注意到配置文件NLog.config在Azure中未得到更新(或正在被覆盖)。 Renaming the configuration file to something different solved my problem. 将配置文件重命名为其他文件解决了我的问题。 However, I consider this a workaround only. 但是,我认为这只是一种解决方法

The NLog.config in Azure seems to be the one coming as example, and contains no targets at all. Azure中的NLog.config似乎是一个示例,它根本不包含任何目标。

It looks like there are some transformation issues & nothing to do with Azure website. 似乎有一些转换问题,与Azure网站无关。

I would suggest following ways to fix it more permanently: 我建议采用以下方法更永久地修复它:

  1. Easy Way : Cleate nlog.build.config for each environment & set "Copy to Output Directory" to Always. 简单方法 :为每个环境选择nlog.build.config并将“复制到输出目录”设置为“始终”。 Please do not add any transformation in the this configs. 请不要在此配置中添加任何转换。 In web.build.config, you just add transform different nlog files. 在web.build.config中,您只需添加转换不同的nlog文件。
  2. Alternative to Easy Way : You can always copy your code from nlog.config into web.config & provide transformation in web.build.config files. 简便方法的替代方法 :您始终可以将代码从nlog.config复制到web.config,并在web.build.config文件中提供转换。
  3. Hard Way: You can use tools like slow cheetah to do actual transformation. 艰难的方式:您可以使用诸如慢猎豹之类的工具进行实际的转化。 https://www.c-sharpcorner.com/article/transform-config-using-slow-cheetah/ . https://www.c-sharpcorner.com/article/transform-config-using-slow-cheetah/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM