2

When saving logs into Azure Blob Storage, is there a way to create a different folder per day? Right now I'm using this configuration, and it works fine

<appender name="AzureAppender2" type="log4net.Appender.AzureBlobAppender, log4net.Appender.Azure">
     <param name="ContainerName" value="testcon" />
     <param name="DirectoryName" value="myfolder/logs.txt" />
     <param name="ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=testcon;AccountKey="rftgdfgdfgfdg78=="/>
     <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
</appender>

I've already tried this

<param name="DirectoryName" value=%date/" />

But it doesn't work

What I want is to be able to dynamically use different folders per day: something like

DirectoryName = 2016-05-13

DirectoryName = 2016-05-12

DirectoryName = 2016-05-11

Is this achievable?

Thanks

Mihai
  • 2,740
  • 31
  • 45
  • I have not tried it personally but based on a quick search (this for example: http://stackoverflow.com/questions/571876/best-way-to-dynamically-set-an-appender-file-path), can you try something like ``? – Gaurav Mantri May 13 '16 at 03:48
  • Thanks. I've tried that one, doesn't work :) – Mihai May 13 '16 at 04:00

1 Answers1

6

Here's the code for the appender on GitHub.

Here's the DirectoryName class property that maps from the configuration value:

   private string _directoryName;

    public string DirectoryName
    {
        get
        {
            if (String.IsNullOrEmpty(_directoryName))
                throw new ApplicationException(Resources.DirectoryNameNotSpecified);
            return _directoryName;
        }
        set
        {
            _directoryName = value;
        }
    }

And the relevant Filename method that actually creates the file name for the blob:

private static string Filename(LoggingEvent loggingEvent, string directoryName)
        {
            return string.Format("{0}/{1}.{2}.entry.log.xml",
                                 directoryName,
                                 loggingEvent.TimeStamp.ToString("yyyy_MM_dd_HH_mm_ss_fffffff",
                                                                 DateTimeFormatInfo.InvariantInfo),
                                 Guid.NewGuid().ToString().ToLower());
        }

So it looks like directoryName only accepts static values. Good thing it's open source ...

Kyle Hale
  • 7,912
  • 1
  • 37
  • 58
  • Thanks. I saw that also, but I was hoping for a way of doing it without having to modify the code myself :) – Mihai May 13 '16 at 05:32