【问题标题】:Upload a file generated from Web Services project to Azure Blob Storage将从 Web 服务项目生成的文件上传到 Azure Blob 存储
【发布时间】:2017-07-16 17:27:45
【问题描述】:

目前我有一个 Web 服务项目,在启动代码中我从 Serilog 创建了一个记录器,如下所示:

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();
        }

当 Web 应用程序关闭时,将在项目域文件夹中本地创建一个日志文件。我想使用 Azure Blob 存储上传此文件。我的问题是,我无法知道应用程序何时会因任何原因关闭,比如说一个错误或者开发人员是否已经使用它。我想将他们会话中的所有日志上传到 blob。 Web 服务代码中是否有一个区域会在它关闭之前进入该区域,以便我可以将上传文件放在那里?

谢谢!

*** 编辑****

我在全局文件中添加了一个方法:

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


        }

【问题讨论】:

    标签: c# web-services file-upload azure-blob-storage serilog


    【解决方案1】:

    我建议您可以尝试添加 Global.asax 文件。 据我所知,我们可以使用 Application_End 方法来达到这个目的。

    Application_End 在 ASP.NET 工作进程终止时触发。这通常发生在一段可配置的不活动时间之后或 IIS(或相关的应用程序池)关闭或重新启动时。

    我已经在我的电脑上编写了一个测试演示。 我使用 Application_Start 方法开始记录详细信息。我用来将文件参数传递给 application_end 的静态变量“text”。

    更多细节,您可以参考以下代码:

    全球.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);
                }
            }
        }
    

    结果:

    希望这能给你一些建议。

    【讨论】:

    • 我自己添加了这个方法,只是想看看它是否会在网络浏览器关闭后执行该方法,但它没有。有什么我做错了吗?我会把我改变的东西放在 OP 中
    • 我可以让应用程序到达 Application_End 的唯一方法是停止 IIS Express。我正在寻找可以让我在跑步时按停止就能到达那里的东西。
    猜你喜欢
    • 2022-01-18
    • 2015-05-20
    • 2021-03-07
    • 2017-01-24
    • 2017-08-19
    • 2012-02-03
    • 2019-04-10
    • 2019-07-10
    • 1970-01-01
    相关资源
    最近更新 更多