public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration(ConfigureConfiguration) .ConfigureLogging((hostingContext, logging) => { var c = hostingContext.Configuration; var logLevel = Parse.ParseEnum <Serilog.Events.LogEventLevel>(c["Logging:LogLevel:Default"], Serilog.Events.LogEventLevel.Warning); var loggerConfiguration = new LoggerConfiguration() .ReadFrom.Configuration(c) .MinimumLevel.Is(logLevel) .Enrich.With <Serilog.Enrichers.MachineNameEnricher>() .Enrich.With <Serilog.Enrichers.ProcessIdEnricher>() .Enrich.With <Serilog.Enrichers.ThreadIdEnricher>() .Enrich.With <Serilog.Enrichers.CorrelationIdEnricher>() .Enrich.WithProperty("ApplicationName", c["ApplicationName"]) .Enrich.WithProperty("SprintName", c["SprintConfig:SprintName"]) .Enrich.FromLogContext() .WriteTo.Trace(); var connectionString = c.GetConnectionString(c["SerilogSqlServerSink:ConnectionStringName"]); if (!string.IsNullOrEmpty(connectionString)) { loggerConfiguration.WriteTo.MSSqlServer(connectionString, c["SerilogSqlServerSink:TableName"], schemaName: c["SerilogSqlServerSink:SchemaName"]); } var appName = c[GsaAppSettingsVariablePaths.AppName]; var gas = c.GetGsaAdministrativeSettings(); if (!string.IsNullOrEmpty(gas.LogFileDirectory)) { try { if (File.Exists(gas.LogFileDirectory)) { FileSystemHelpers.FileTryDelete(gas.LogFileDirectory); } if (!Directory.Exists(gas.LogFileDirectory)) { Directory.CreateDirectory(gas.LogFileDirectory); } var logRotatingFileName = Path.Combine(gas.LogFileDirectory, $"{appName}.log"); EnvironmentInfo[nameof(logRotatingFileName)] = logRotatingFileName; loggerConfiguration.WriteTo.RollingFile(logRotatingFileName, logLevel, flushToDiskInterval: TimeSpan.FromSeconds(5)); } catch (Exception ex) { EnvironmentInfo["OnDiskLogSetupError"] = ex.Message; } } Log.Logger = loggerConfiguration.CreateLogger(); logging.AddSerilog(); Log.Information( @"Logging Initialized for: {appName} {LogFileDirectory} {TempFileDirectory} {AppSettingsDirectory} {AttachmentDirectory}", appName, gas.LogFileDirectory, gas.TempFileDirectory, gas.AppSettingsDirectory, gas.AttachmentDirectory ); }) .UseStartup <Startup>();
//[ValidateAntiForgeryToken] public async Task <JsonResult> Save(int?documentId, string documentName, int workflowId, string newRemovedAttachmentIds) { try { if (!ModelState.IsValid) { throw new ArgumentException(ModelState.ToString(), nameof(ModelState)); } await CheckStalenessFromFormAsync(); Document document; if (documentId.GetValueOrDefault(0) > 0) { document = await DB.Documents.FindAsync(documentId); if (document == null) { throw new KeyNotFoundException($"could not find documentId={documentId}"); } document.DocumentDocumentDocumentTypes.ToList().ForEach(dt => DB.DocumentDocumentTypes.Remove(dt)); } else { document = new Document { UploadedByUserId = CurrentUserId, WorkflowId = workflowId, //CreatedAtUtc = DateTime.UtcNow }; DB.Documents.Add(document); } document.DocumentName = StringHelpers.TrimOrNull(documentName); var documentTypeIds = CSV.ParseIntegerRow(Request.Query["documentTypeId"]); var documentTypeNames = new List <string>(); var d = PopulateDocumentTypeNameByDocumentTypeIdInViewBag(); foreach (var id in documentTypeIds) { DB.DocumentDocumentTypes.Add(new DocumentDocumentType { DocumentTypeId = id, Document = document }); documentTypeNames.Add(d.FindOrDefault(id)); } await DB.SaveChangesAsync(); //DB.Refresh(document); var attachmentsTempData = TempData.FileUploadAttachmentResults(); if (attachmentsTempData.Count > 0) { var rids = CSV.ParseIntegerRow(newRemovedAttachmentIds); foreach (var tempAttachment in attachmentsTempData) { if (!rids.Contains(tempAttachment.AttachmentsId)) { var folder = await SpecialFolderProvider.GetDocumentFolderAsync(document.DocumentId); var file = await folder.CreateFileAsync(Path.GetFileName(tempAttachment.FilePath)); using (var st = await file.OpenWriteAsync()) { using (var tmp = System.IO.File.OpenRead(tempAttachment.FilePath)) { await tmp.CopyToAsync(st); } } FileSystemHelpers.FileTryDelete(tempAttachment.FilePath); var attachment = new Attachment { FileName = tempAttachment.FileName, FilePath = file.Path, DocumentId = document.DocumentId, FileSize = tempAttachment.FileSize, ContentType = tempAttachment.ContentType, CreatedByUserId = tempAttachment.CreatedByUserId }; DB.Attachments.Add(attachment); } } await DB.SaveChangesAsync(); TempData.ClearFileUploadAttachmentResults(); } return(Json(new { Id = document.DocumentId, UserName = User.Identity.Name, Name = document.DocumentName, DocumentTypeNames = documentTypeNames.WhereNotNull().OrderBy().ToList(), AttachmentCount = document.DocumentAttachments.Count, UploadedDate = PortalHelpers.ToLocalizedDisplayDateString(document.CreatedAtUtc) })); } catch (Exception ex) { return(base.CreateJsonError(ex)); } }