Is there a way to configure multiple Serilog RollingFiles through appSetting configuration - serilog

Is there a way to configure multiple Serilog RollingFiles through appSetting?
I want to create separate log files for Information and Error levels.

Configuring multiple logs is pretty simple in appsettings, mission is to use filter. i spent almost 3hours trying to figure out how to archieve this.
I ended up doing the following:
Startup.cs / Global.asax.cs
Log.Logger = new LoggerConfiguration()
.WriteTo
.Logger(x => x.Filter
.ByIncludingOnly(logEvent => logEvent.Level == Serilog.Events.LogEventLevel.Error)
.ReadFrom
.AppSettings("error"))
.WriteTo
.Logger(x => x.Filter
.ByIncludingOnly(logEvent => logEvent.Level == Serilog.Events.LogEventLevel.Information)
.ReadFrom
.AppSettings("info")).CreateLogger()
Web.Config
<add key ="error:serilog:using:RollingFile" value="Serilog.Sinks.RollingFile"/>
<add key ="error:serilog:write-to:RollingFile.pathFormat" value="C:\log\error {Date}.txt"/>
<add key ="error:serilog:write-to:RollingFile.formatter" value="Serilog.Formatting.Json.JsonFormatter"/>
<add key ="info:serilog:using:RollingFile" value="Serilog.Sinks.RollingFile"/>
<add key ="info:serilog:write-to:RollingFile.pathFormat" value="C:\log\info {Date}.txt"/>
<add key ="info:serilog:write-to:RollingFile.formatter" value="Serilog.Formatting.Json.JsonFormatter"/>

Not directly - it's possible to use a setting prefix like:
.ReadFrom.AppSettings()
.ReadFrom.AppSettings(settingPrefix: "2")
And then add the additional sink like:
<add key="2:serilog:write-to:RollingFile.pathFormat" value="..." />
Baking this properly into the app settings configuration provider has been a "TODO" for a while.
If configuring the sinks in code is possible, that's probably the way to go.

Related

Servicestack MySql connection string

I'm trying to figure out how to create a connection string in Servicestack for (in this case) MySql.
The question is: what is the connection string in Web.config supposed to look like ?
I stumbled on two ways:
1)
<appSettings>
<add key= "ConnectionString"
value= "Uid={User};
Password={Password};
Server= {EndpointUrl};
Port= {EndpointPort};
Database= customers" />
</appSettings>
and
2)
<connectionStrings>
<add name="testDb"
connectionString=
"Server= localhost;
Database= test;
UID= root;
Password= test"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
The question above is due to the fact that I have had a hard time to find an answer to this (apparently trivial) question. The answers I find are usually answering more complicated problems.
The Servicestack dokumentation looks like it's tuned to a rather more experienced audience than myself. Not by much but just, I'm a rookie at this.
This is a general problem I guess, in any documentation of a system. It's easy to go blind when it comes to fundamentals. What's self-evident to me is complicated for the next person.
A connection string should contain your database connection in a single string, this is an example of a MySql connection string to the “test” database on “localhost”:
Server=localhost;Database=test;UID=root;Password=test;SslMode=none
You can use this as a template and replace it with the parameters with your database info.
You can add this in your web.config with:
<add key="ConnectionString"
value="Server=localhost;Database=test;UID=root;Password=test;SslMode=none" />
Which you can then access with ServiceStack's AppSettings API:
container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(
AppSettings.GetString("ConnectionString"), MySqlDialect.Provider));

How to override Elmah applicationname set in Web.config

We have a multi-tenanted MVC app, meaning that exactly the same app is published to multiple IIS virtual directories / applications, and then the app its self works out who it is, and skins its self (css) accordingly.
This is all very well, but anything logged by ELMAH in our elmah database gets logged under the same applicationName, as this is pulled out of Web.Config elmah section below where everything would be logged as "MyappName" :
<configuration>
[...]
<elmah>
<security allowRemoteAccess="false" />
<errorLog
type="Elmah.SqlErrorLog, Elmah"
connectionStringName="elmah"
applicationName="MyappName" />
</elmah>
</configuration>
The question is therefore how to override the applicationName setting from web.config with something specific so we can distinguish errors for a given tenant web site.
As this is configurable within the web.config, ELMAH are already providing you with a way to specify the application name when the application is deployed to different locations - it's just a case of making use of it.
This would generally be something that you would manipulate as part of your deployment steps. If you are doing it manually then it's going to be a pain, but it could be easily manipulated by using a web.config transform.
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<elmah>
<errorLog applicationName="MyappName" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</elmah>
</configuration>
I wonder if the following might work, if you put the following into your Global.asax:
var service = ServiceCenter.Current;
ServiceCenter.Current = context =>
{
var connectionString = "YOUR CONNECTION STRING";
var container = new ServiceContainer(service(context));
var log = new SqlErrorLog(connectionString) { ApplicationName = "APP NAME HERE" };
container.AddService(typeof(ErrorLog), log);
return container;
};

How to set formatProvider property in Serilog from app.config file

I know that it's possible to setup Serilog sinks in app.config file (AppSettings section) and it's pretty simple with scalar types, but how to be with complex ones (IFormatProvider etc.). Does anybody know how to deal with that and is it possible at all?
I'm trying to simulate this example
ILogger logger = new LoggerConfiguration()
.Enrich.WithExceptionDetails()
.WriteTo.Sink(new RollingFileSink(
#"C:\logs",
new JsonFormatter(renderMessage: true))
.CreateLogger();
but using app.config only.
You can use something like JsonRollingFile for this.
<configuration>
<appSettings>
<add key="serilog:using:Json" value="Serilog.Sinks.Json" />
<add key="serilog:write-to:JsonRollingFile.pathFormat" value="C:\Logs\myapp-{Date}.jsnl" />
</appSettings>
</configuration>

How to program ASP.NET web.config FederationMetadata appSettings keys?

Is there a way to programmatically create the "ida" keys in
below?
<add key="ida:FederationMetadataLocation" value="http://xxxxx/FederationMetadata" />
<add key="ida:Issuer" value="http://xxxxxx/Issuer" />
<add key="ida:ProviderSelection" value="productionSTS" />
Configuration.AppSettings is a writable collection so you can certainly do this . These keys, however are only needed for the "identity and access" visual studio plugin. AFAIK they are never used at runtime so there is little use in creating them.

asp.net mvc: disabled RoleManager is still executed

I have a web site where configured with:
<roleManager enabled='false'></roleManager>
But I still can see roleManager being executed in the pipeline (by looking at traces, also I am getting exception when roleManager tries to load roles from SQL providers configured in machine.config)
How can I disable roleManager?
fix
add enableSimpleMembership with value false app setting to your web.config.
cause
<roleManager enabled="false" />
will cause Roles.Enabled flag to be set to false, as expected,
but there is WebMatrix.WebData.WebSecurity that says:
internal static void PreAppStartInit()
{
if (!ConfigUtil.SimpleMembershipEnabled)
return;
...
Roles.Enabled = true;
...
}
this will override roleManager setting (this code is executed before RoleManager module is).
to disable SimpleMembership you can add app setting enableSimpleMembership with value="false" (web.config):
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="enableSimpleMembership" value="false" />
</appSettings>
</configuration>
this will prevent webmatrix from enabling RoleManager.
Another solution (hack) is to remove RoleManager module from the list of modules:
....
<system.webServer>
<modules>
<remove name="RoleManager"/>
</modules>
....
no... I don't think it's correct, even with enableSimpleMembership=false, you'd still need to implement a Dummy RoleProvider, otherwise, exception "The Role Manager feature has not been enabled."
Here's how to implement a dummy RoleProvider:
Turning off only the Role Provider in SimpleMembership

Resources