Good day folks
I must have read about 20 different articles/approaches about how to implement log4net in ASP.NET 1.0 / 2.0 and various other application types.
Link #1
Link #2
Link #3
Well, above articles are all ok, i did try/follow all of them, but i never got a log file..
What i have:
IIS Hosted WCF Service.
What i want:
Logging using log4net - well, first i wanted to use Tracing (like Trace.Write in System.Diagnostics) then the Microsoft Enterprise Library and finally i'm stuck trying to figure out how to setup/configure log4net for my logging needs.
My question:
How to get log4net writing a logfile? i mean:
- is it a security problem? do i really need to procmon this?
- is the problem to write to a log file (security)?
- is the problem that the configuration file cannot be read?
i tried:
Specifying the log4net.config using
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "log4net.config", Watch = true)]
Specifying the configuration in web.config after the block. When i did that i wasn't able to run the Web Application anymore (IIS reports corrupt web.config).
Help in this issue is really appreciated!
Christian
Ok, my fault. It was the declaration when ConfigFileExtension is declared, log4net searches for .dll. in the web root. Just changed that to ConfigFile and it's working like a charm. my careless mistake...!
Christian
Related
Today at work I was asked to add some basic logging functionality in my existing ASP.NET MVC (not Core) project. I was told to use Serilog for this.
However, I failed to find any reasonable example of using Serilog in MVC, all links simply refer to Core. I tried to do everything myself. After installing all necessary packages (Serilog, Serilog.Sinks.File), I created a "Logs" folder and tried this:
var log = new LoggerConfiguration()
.WriteTo.File("~/Logs/log.txt")
.CreateLogger();
log.Information("Hello from Serilog!");
I expected to see a new file in the folder but there wasn't any. I also tried using a simpler path "log.txt" and creating a file in this folder preemptively but it didn't help.
Is it possible that Serilog is not supported for MVC at all? If it is, what is a simple way to log something into a text file?
I doubt Serilog understands the ~/ syntax in your path to your log file. ~/ refers to the virtual root of your site, but Serilog doesn't know anything about virtual roots or websites. It's just a logging framework and expects a normal path. So if you want to log to a file called log.txt in the Logs directory of your site, then you need to convert it to a proper path that Serilog can understand.
The System.Web.Hosting.HostingEnvironment.MapPath method can translate virtual root relative paths. So ~/Logs/log.txt can become something like C:\path-to-your-site\Logs\log.txt.
Change to this:
var log = new LoggerConfiguration()
.WriteTo.File(System.Web.Hosting.HostingEnvironment.MapPath("~/Logs/log.txt"))
.CreateLogger();
log.Information("Hello from Serilog!");
//note that importing the namespace will make it so you don't have to fully qualify the name.
Then Serilog should log to the location you're expecting, if the directory exists and it has write privileges there.
File logging from a web application is going to get very messy. Unless your site gets hardly any usage, the logs are going to become unmanageable and useless. I suggest you look at the other Serilog sinks, and find one that logs to a logging service such as Seq (from the makers of Serilog) or Stackify Retrace.
We use WCF client in our project which is an Azure functions app, to communicate with an external web service. We need to change the xmlSerializer's tempFilesLocation because of the permission issue. I searched online and found the following configuration that we can use in our web.config which will solve the problem.
<system.xml.serialization>
<xmlSerializer tempFilesLocation="an absolute path of your choice"/>
</system.xml.serialization>
But in Azure Functions app, we don't have access to web.config, so we need to find a way to do it in the code. Is there any way to change tempFilesLocation in the code?
It's not possible to modify the web.config for functions running on the dynamic sku (where you pay-per-invocation).
However, if you create your function on the non-dynamic/classic sku (where you pay per vm, the pricing model for regular web apps) then you can modify the web.config settings via an applicationHost.xdt file. More details on how to work with xdt file here: https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples
I'm using the AllScripts HelloWorld TouchWorks C# Project, as provided on their site. I've also created a valid application, using the svcUsername and svcPassword on the app.config file to log and added my own appName instead of the default web20.
Didnt change anything else, tried with many different application ID's, all trying to log into the default sandbox server in the example and some trying other sandboxes.
Regardless of what I do, I keep getting
Error: Service Application not licensed on this server!
Despite this being a sandbox server, thus suppouseably accessable to all applications.
What did I do wrong?
edit: Tried to do the same in Slueth, I get the same error.
It's more simple than it seems. Sandbox servers DO require licenses. Talked with the staff, they're nice people, so they manually added me after a few explanations of my requirements.
In an Azure Websites I was always using the following code to fetch some values from the config's app settings:
string property = WebConfigurationManager.AppSettings["property"];
Just a couple of days ago I stublemd upon CloudConfigurationManager, and with it I can get the property like so:
string property = CloudConfigurationManager.GetSetting("property");
Although CloudConfigurationManager seems like it's better fitted to cloud use, I never had any issues with WebConfigurationManager.
Should I be using CloudConfigurationManager?
What are the differences between the two?
In what cases CloudConfigurationManager will behave diffrent from
WebConfigurationManager?
CloudConfigurationManager enables us to read configuration file regardless of the environment we are in.
So instead of writing environment specific code statements e.g., for web.config file:
WebConfigurationManager.AppSettings["MySetting"]
For ServiceConfiguration.cscfg file:
RoleEnvironment.GetConfigurationSettingValue("MySetting")
We can write the below statement, which will read values from all the configuration files i.e., app.config, web.config and ServiceConfiguration.cscfg.
CloudConfigurationManager.GetSetting("MySetting")
CloudConfigurationManager requires Microsoft.WindowsAzure.Configuration assembly, part of Azure SDK or separate NuGet.
WebConfigurationManager requires System.Web.Configuration assembly, part of .NET Framework.
WebConfigurationManager and CloudConfigurationManager manage different configuration files.
WebConfigurationManager is for managing website's web.config file(s) and it's appsettings and connections strings
CloudConfigurationManager is for managing .cscfg files (for cloud services). His benefit is that you can manage configurations and connections from the azure portal directly.
I think you're better off using WebConfigurationManager.
With it, you have access to ConnectionStrings as well as AppSettings.
Both sets of settings you can update via the Azure Portal. They can then further be used in other Azure facilities/services, such as when configuring website backup.
Check this out for more information: https://azure.microsoft.com/en-us/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/
I am currently running a WCF service on an AppFabric server and my application needs to load a the web.config file dynamically to retrieve custom configuration sections.
On my development machine I can just load the configuration like this:
WebConfigurationManager.OpenMappedWebConfiguration(webMappedFile, virtualPath);
But on the test machine (AppFabric server) I am getting an exception and it seems that I need to specify a third parameter which is actually the site the web application is running on:
WebConfigurationManager.OpenMappedWebConfiguration(webMappedFile, virtualPath, "MySite");
So I tried to hard code it and it worked. Anyway this is not acceptable, so I need to dynamically provide the site to the WebConfigurationManager because I do not on which site the service will be running in the future. Do anybody knows how to achieve that?
Thanks.
If you are running this code as part of handling a request you could use:
Request.ServerVariables("server_name")
see: http://msdn.microsoft.com/en-us/library/ms525396(VS.90).aspx
Edit based on your comment
The parameter that you need is the Site Name, not the machine name, your code be running on many machines. If the code is running somewhere where it no longer knows that it is on a web site, then it is difficult for it to get the name of the web site that it is running on.
You then have two options:
Send the name as a parameter from a layer that has httpconext
Not sure if this will work: but you could try adding a reference to system.web to your project. It may compile, but you could get a null reference exception when you run it. Probably worth a try.
How about Server.MachineName