![](/img/trans.png)
[英]Upload to Azure Media Services / blob storage with SasLocator not showing uploaded file
[英]Upload a file generated from Web Services project to Azure Blob Storage
目前我有一个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();
}
我建议你可以尝试添加Global.asax文件。 据我所知,我们可以使用Application_End方法来实现此目的。
ASP.NET工作进程终止时触发Application_End。 这通常发生在可配置的不活动时间段或IIS(或相关应用程序池)关闭或重新启动后。
我在我的电脑上写了一个测试演示。 我使用Application_Start方法开始记录详细信息。 我用来将文件参数传递给application_end的静态变量“text”。
更多细节,您可以参考以下代码:
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);
}
}
}
希望这可以给你一些提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.