1

Currently i have a web services project, in the startup code I have created a logger from Serilog like so:

public Startup()
        {
            Log.Logger = new LoggerConfiguration()
               .MinimumLevel.Debug()
               .WriteTo.File(AppDomain.CurrentDomain.BaseDirectory + "\\logs\\logs--"+ string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now) + ".log", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}",shared:true)
               .CreateLogger();
        }

When the web application closes, the a log file will be created locally in the project domain folder. I would like to upload this file using Azure Blob Storage. My problem is that, I have no way of no knowing when the application will shut down for whatever reason, lets say an error or if the developer is done using it. I would like to upload all the logs from their session to a blob. IS there an area in web service code where it will go to before it closes so I can put the upload there?

THank you!

*** Edit ****

I added a method in the Global File:

protected void Application_End()
        {
            Log.Debug("We made it to application end");
            Log.CloseAndFlush();


        }
JPhillips
  • 59
  • 1
  • 5

1 Answers1

0

I suggest you could try to add Global.asax file. As I know we can use Application_End method to achieve this purpose.

Application_End is triggered when the ASP.NET worker process terminates. This usually occurs after a configurable period of inactivity or when IIS (or the relevant application pool) is shut down or restarted.

I have written a test demo on my computer. I used Application_Start method to start log the details. The static variable “text” I used to pass the file parameter to application_end.

More details, you could refer to below codes:

Global.asax:

public class Global : System.Web.HttpApplication
    {
           static string text;
        protected void Application_Start(object sender, EventArgs e)
        {
             text = AppDomain.CurrentDomain.BaseDirectory + "\\logs\\logs--" + string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now) + ".log";

            Log.Logger = new LoggerConfiguration()
              .MinimumLevel.Debug()
              .WriteTo.File(text, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}", shared: true)
              .CreateLogger();
            Log.Information("Hello, world!");

            int a = 10, b = 0;
            try
            {
                Log.Debug("Dividing {A} by {B}", a, b);

            }
            catch (Exception ex)
            {
                Log.Error(ex, "Something went wrong");
            }
        }

        protected void Application_End(object sender, EventArgs e)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                         "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();


            CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
            string text2 = text;

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now) + ".log");

            Log.CloseAndFlush();


            using (var fileStream = System.IO.File.OpenRead(text))
            {
                blockBlob.UploadFromStream(fileStream);
            }
        }
    }

Result: enter image description here

Hope this could give you some tips.

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65
  • I added the method by itself just to see if it will go the method once the web browser has closed but it hasn't. Is there something I am doing it wrong? Ill put what I change in the OP – JPhillips Mar 04 '17 at 23:42
  • The only way I can get application to reach Application_End is by stopping IIS Express. I'm looking for something that will enable me to reach there just by pressing stop on the run. – JPhillips Mar 05 '17 at 22:11