Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

NLog not writing to Azure Blob Storage

I'm new with Azure and I'm trying to get my first MVC Core 3.1 application on Azure to use NLog to write to an Azure Blob Storage. I believe I have it setup correctly but I'm not seeing anything in my Blob Storage.

I'm using the following articles to help.

https://www.taithienbo.com/securely-log-to-blob-storage-using-nlog-with-connection-string-in-key-vault https://ozaksut.com/custom-logging-with-nlog

When I look at my Blob Storage I don't see any files. I'm also assuming I have my Blob Storage setup correctly.

Here is a snippet of my proj file to show I have what should be the correct NLog packages.

<ItemGroup>
        <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.10" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.10">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
        <PackageReference Include="NLog.Extensions.AzureBlobStorage" Version="3.0.0" />
        <PackageReference Include="NLog.Web.AspNetCore" Version="4.9.2" />
        <PackageReference Include="Oracle.EntityFrameworkCore" Version="3.19.80" />
        <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
        <PackageReference Include="System.ServiceModel.Duplex" Version="4.4.*" />
        <PackageReference Include="System.ServiceModel.Http" Version="4.4.*" />
        <PackageReference Include="System.ServiceModel.NetTcp" Version="4.4.*" />
        <PackageReference Include="System.ServiceModel.Security" Version="4.4.*" />
    </ItemGroup>

    <ItemGroup>
        <Content Update="nlog.config">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
    </ItemGroup>

Below is my NLog.config file. If I remove the throwExceptions="true" then the website will come up but no log files are created. If I leave the expections on then my website errors out and I see an error in the Azure logs that says Unhandled exception. System.FormatException: Settings must be of the form "name=value". along with

at NLog.Targets.BlobStorageTarget.CloudBlobService.Connect(String connectionString, String serviceUri, String tenantIdentity, String resourceIdentity, IDictionary`2 blobMetadata, IDictionary`2 blobTags)
   at NLog.Targets.BlobStorageTarget.InitializeTarget()
   at NLog.Targets.Target.Initialize(LoggingConfiguration configuration)
   at NLog.Targets.Target.NLog.Internal.ISupportsInitialize.Initialize(LoggingConfiguration configuration)
   at NLog.Config.LoggingConfiguration.InitializeAll()
   at NLog.LogFactory.ReconfigExistingLoggers()
   at NLog.LogFactory.set_Configuration(LoggingConfiguration value)
   at NLog.LogFactory.LoadConfiguration(String configFile)
   at NLog.LogManager.LoadConfiguration(String configFile)
   at NLog.Web.NLogBuilder.ConfigureNLog(String configFileName)

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"
      throwExceptions="true">

  <!-- enable asp.net core layout renderers -->
  <extensions>    
    <add assembly="NLog.Extensions.AzureBlobStorage" />
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <variable name="LogDirectory" value="D:logsMvcProjectName"/>

  <!-- the targets to write to -->
  <targets>
    
    <target xsi:type="File" 
      name="DefaultTarget" 
      fileName="${LogDirectory}LogFile.log"
      layout="${longdate} | ${uppercase:${level}} | ${callsite} | ${message}"
      archiveAboveSize="3145728"  
      archiveEvery="Day"
      archiveFileName = "${LogDirectory}/Archive/{#}.log"  
      archiveNumbering = "DateAndSequence"            
      archiveDateFormat = "yyyyMMdd"  
      maxArchiveFiles = "50"            
    />

    <target xsi:type="AzureBlobStorage"
        name="AzureBlob"
        blobName="Log-${shortdate}.log"
        container="epays-log"
        connectionString="https://myservername.blob.core.windows.net/epays-log"
        layout="${longdate} | ${uppercase:${level}} | ${callsite} | ${message}">
    </target>

    <target name="ConsoleTarget"
      xsi:type="Console"
      layout="${longdate} ${logger:shortName=True} ${message} ${onexception:EXCEPTION OCCURRED:${exception:format=type,message,StackTrace,method:maxInnerExceptionLevel=8:innerFormat=type,message,StackTrace,method}}"
    />
    
  </targets>  
  <rules>
    <logger name="Microsoft.*" maxlevel="Info" final="true" /> <!-- BlackHole without writeTo -->
    <logger name="*" minlevel="Debug" writeTo="DefaultTarget" />
    <logger name="*" minlevel="Debug" writeTo="AzureBlob" />
    <logger name="*" minlevel="Debug" writeTo="ConsoleTarget" />
  </rules>
</nlog>

When I run the site locally, I'm able to get it to log to the local file structure using the "DefaultTarget" settings.

question from:https://stackoverflow.com/questions/65905065/nlog-not-writing-to-azure-blob-storage

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Is seems your configuration file is correct but you didn't find where your connection string is.

Go to your storage account page, find Access Key under settings, copy the connection string to your nlog.config file.

enter image description here

Here is a sample on my side:

  1. nlog.config file content:

     <?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"
           internalLogLevel="info"
           internalLogFile="C:empfallback-log.txt"
           throwConfigExceptions="true">
    
       <extensions>
         <add assembly="NLog.Web.AspNetCore"/>
         <add assembly="NLog.Extensions.AzureBlobStorage" />
       </extensions>
    
       <targets async="true">
         <target
           xsi:type="AzureBlobStorage"
           name="azure"
           layout="${longdate:universalTime=true} ${level:uppercase=true} - ${logger}: ${message} ${exception:format=tostring}"
           connectionString="your-connection-string-goes-here"
           container="logs"
           blobName="${date:universalTime=true:format=yy-MM-dd}.log" />
       </targets>
    
       <rules>
         <logger name="*" minlevel="Trace" writeTo="azure" />
       </rules>
    
     </nlog>
    
  2. The logs file generated by project:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...