NLog configuration from both code and config file - smtp

I have NLog code currently reading from NLog.config to set up logging to text files and then I'd like to add logging fatal errors to email. However, the email settings are in my appsettings.json file.
What I tried so far is this in my Startup.cs
var emailConfig = Configuration
.GetSection("EmailConfiguration")
.Get<EmailConfiguration>();
services.AddSingleton(emailConfig);
var mailTarget = new MailTarget()
{
SmtpServer = emailConfig.SmtpServer,
SmtpUserName = emailConfig.UserName,
SmtpPort = emailConfig.Port,
SmtpPassword = services.BuildServiceProvider().CreateScope().ServiceProvider.GetService<IEmailSender>().Decrypt(emailConfig.Password),
To = emailConfig.To
};
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(mailTarget, LogLevel.Fatal);
However I have 2 problems when I try to _logger.Fatal("testing, please ignore");:
Under _logger.Factory.Configuration.AllTargets I now only see the mail settings I configured above, which I understand is due to SimpleConfigurator overwriting (yet I'm not sure what I need to do so that I add rather than overwrite).
I still didn't receive an email despite and I'm not sure how I can debug this now.

I fixed both issues.
var mailTarget = new MailTarget()
{
SmtpServer = emailConfig.SmtpServer,
SmtpUserName = emailConfig.UserName,
SmtpPort = emailConfig.Port,
SmtpPassword = services.BuildServiceProvider().CreateScope().ServiceProvider.GetService<IEmailSender>().Decrypt(emailConfig.Password),
From = emailConfig.UserName,
To = emailConfig.To,
EnableSsl = true,
SmtpAuthentication = SmtpAuthenticationMode.Basic,
Html = true
};
var configuration = LogManager.Configuration;
configuration.AddRuleForOneLevel(LogLevel.Fatal, mailTarget);
LogManager.ReconfigExistingLoggers();
The first problem related to the last 3 lines, whereas the second issue was related to the SMTP configuration.

Related

Getting error when converting multipage fillable PDF to html form using Abcpdf

The exception thrown was " at WebSupergoo.ABCpdf10.Doc.Save(String path)
at GetHtmlFromUploadedPdfDocument(Nullable`1 pageNumber) in....." .
The uploaded pdffile contains barcodes and fillable fext fields .
Below is the code i used to convert pdf to html.
var filePaths= HttpContext.Current.Server.MapPath("~/PDF//");
byte[] bytes = File.ReadAllBytes(filePaths);
doc.Read(bytes);
if (pageNumber > 0)
{
doc.PageNumber = pageNumber.Value;
doc.RemapPages(pageNumber.ToString());
}
var pdfFile = "sample";
var htmlPath = HttpContext.Current.Server.MapPath("~/HTML/" + pdfFile + ".html");
doc.Encryption.CanChange = false;
doc.Encryption.CanEdit = false;
doc.Encryption.CanAssemble = false;
doc.Encryption.CanExtract = false;
doc.Encryption.CanFillForms = false;
doc.Save(htmlPath);
content = File.ReadAllText(htmlPath);
I know this is old post. But i faced similar issue.
I solved it eventually, it might help others.
In my case, folder under which I was saving the file was missing proper permission.
Please perform following task:
Right click on root folder where you are trying to save file.
Select Properties. Uncheck Read-only in attributes section.
Go to Security Tab. Select Edit > Add.
Key in "Everyone" in text box. Then Check Names > Ok.
Give "Everyone" Read, Write and Modify permission.

How can I send files / pictures to Report Portal with .net client?

I'd like to send a binary attachment to Report Portal with .net client. How can I do it?
Example is in Tests project.
https://github.com/reportportal/client-net/blob/master/ReportPortal.Client.Tests/LogItem/LogItem.cs
Find CreateLogWithAttach test and see code.
var data = new byte[] { 1, 2, 3 };
var log = Service.AddLogItem(new AddLogItemRequest
{
TestItemId = _testId,
Text = "Log1",
Time = DateTime.UtcNow,
Level = LogLevel.Info,
Attach = new Attach("file1", "application/octet-stream", data)
});
Related documentation: Log Data in ReportPortal

FileReference save to local issue

My requirement is to save a bunch of files (more than 500) in a single zip file locally using FileReference. I am using ASZip to zip the files. Now the problem is if the number of files are more, then I am not even getting Save as dialog box.
I have tried different combinations of data to see whether it is number of files or file size limitation, but it looks like the script automatically stops (irrespective of number of files), if it can't give me the output within a minute.
This is the code that I am using
//*****test code
var myZip:ASZip = new ASZip (CompressionMethod.GZIP);
var myByteArray:ByteArray = new ByteArray();var pdfFile:PDF;
var newPage:Page;
var printPage:BorderContainer;
for (var i:int=0;i<330;i++)
{
printPage = new BorderContainer();
printPage.visible = false;
printPage.x=0;
printPage.y=0;
printPage.includeInLayout = false;
printPage.width = 816+10;
printPage.height = 1056+23;
this.addElement(printPage);
pdfFile = new PDF(Orientation.PORTRAIT, Unit.INCHES, Size.A3 );
pdfFile.setDisplayMode( Display.FULL_PAGE,Layout.SINGLE_PAGE );
newPage = new Page ( Orientation.PORTRAIT,Unit.INCHES,new Size([816+10,1056+10],"MyFavoriteSize",[8.5+10,11+10],[816/0.125,1056/0.218]));
pdfFile.addPage(newPage);
pdfFile.beginFill(new RGBColor(0xFFFFFF));
pdfFile.textStyle(new RGBColor(0x000000));
pdfFile.addImage(printPage,null,-0.5,-0.5,8.5+4,11.5+4);
myByteArray = pdfFile.save(org.alivepdf.saving.Method.LOCAL);
myZip.addFile(myByteArray,i + ".pdf");
}
Can you please let me know what can be done to fix this issue?
Thanks,
Satish.

Box API, Unable to get sharedlink of a file. Shows Bad Request Error

BoxApi.V2.BoxManager boxManager = new BoxManager(response1.Data.access_token);
var sharedLink = new SharedLink()
{
Access = Access.Open,
Permissions = new Permissions() { CanDownload = true, CanPreview = true },
UnsharedAt = DateTime.Now.AddYears(1)
};
boxManager.ShareLink(file, sharedLink);
This below code throws "Bad request" error to me, can anyone help me on this?
Can you verify what type of account you have with Box?
If you have a personal account, removing
UnsharedAt = DateTime.Now.AddYears(1)
should allow the request to work.
Expiring shared links requires an upgraded account

Encrypting configuration sections

I'm trying to programmatically encrypt configuration sections of App.config and Web.config files. In the following code, I set the path configuration file I want to edit in the configFilePath variable and then expect it to encrypt the connectionStrings section.
var config = ConfigurationManager.OpenExeConfiguration(configFilePath);
var section = config.GetSection("connectionStrings");
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
}
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
This runs fine without any errors but makes no changes to the given file. It's like it's not really accessing the file I want to access.
Any ideas?
Right, answering my own question...
The code was indeed not opening the right config file. To do that we need to use ConfigurationManager.OpenMappedExeConfiguration() instead of ConfigurationManager.OpenExeConfiguration().
So, the first line of the code above changes to:
var map = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);