Determine Serilog Enrichers in Use - serilog

I'm trying to write a series of custom enrichers whose values are set dynamically at run-time. In other words, they are not just looking up "static" values from the environment being logged.
To make that work I need to be able to determine which enrichers are present in a given Serilog configuration so that I can write values to them. I've been unable to find a way to list the enrichers that are present. Is it possible to do so?
If there's a different way of being able to inject values dynamically into the pipeline I'd be interested in hearing about it. I've built something to do that using LogContext but I'm hoping to simplify things (which is why I was looking into using enrichers).

Serilog does not expose a way to get the list of Enrichers that have been configured, at runtime. You'd have to create this list yourself at the start of the app, based on the log pipeline code and/or reading the configuration settings.
Alternatively, you could use Reflection if you really want to get this information from the live Serilog configuration, and if you're OK with it possibly breaking in the future as new Serilog versions are released and their internals change.
You can see an example of getting a list of sinks via Reflection on this answer:
Unit test Serilog configuration
Getting a list of Enrichers would be a similar approach.

Related

Swagger best practices

I am currently defining a Rest API and I intend to use Swagger to document it.
I've already started to define my API specifications with the Open Api Specification in YAML into the Swagger-Editor.
Then, I understand that I will provide that file to the Swagger-codegen to generate a server implementation, and also to the Swagger-UI (whose statics files will be previously paste to my server) to expose the interactive documentation .
According to Swagger, this is the top-down approach.
But later, I will probably need to modify this API, and I want to do it via by this tediously YAML file previously defined, to keep the API easily modifiable by anyone (and Language-agnostic).
Does the way to do this is to modify the definition file and then re-use Swagger-codegen ? By this approach, I guess so that I can't even lightly modify the API directly in the implementation server code without risking to have a out of date documentation.
And If I chose to do the bottom-up approach (via Swagger-core annotation), I will restrain all my further modifications to occur in the implementation server code, and my initial definition file will never be usable again.
So another question would be : Is there a common way to deal with Swagger when we want to modify the API both via the specification file and via the implementation server code (I suppose that the file that Swagger-core can generate me from my code, will never looks like my initial one that I defined by hand).
To maintain the API documentation, the best course of action that i can suggest is to follow a hybrid approach.
Initially, when you have to do bulk development, go for the top down approach. This will reduce the initial set up and coding effort. That's the basic idea behind any codegen.
Then, when it comes to maintaining the APIs, or adding a few new ones every day (or week), follow the bottom up approach. You will already have the previous code, the only thing you'll need to do is add some more annotations or API definitions.
Going for top-down iteratively defeats the purpose of code maintenance. Boiler plates and self generated code are there to give you a quick start, not for sustenance.
My opinion may be biased.
For API client, there should not be a need to customize it in most cases. If you find that you need to customize it to meet your requirement, it may worth starting a discussion via https://github.com/swagger-api/swagger-codegen/issues/new (and also please check what are the options available to customize the output, e.g. for PHP, run java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar config-help -l php)
For server stub, ideally the developers only need to focus on the business/application logic and regenerate the server stub when adding/deleting/updating endpoints (but I don't think all the server stubs can achieve that yet)
Disclaimer: I'm the top contributor to Swagger Codegen

How does plugin/plugin dependency work?

I've a very basic question about the practical operation of software plugin systems. I understand how a simple plugin design works, ie one where a plugin adds to a hosting application. Eg a plugin adds a new filter to a paint program. The host knows it has to call a method called filter which the plugin provides. In this case all plugins are independent.
My question relates to the case when one plugin can use the facilities in another plugin. For example there may be a plugin that provides the ability to plot data while another plugin generates data. If the data generator plugin has never seen the graphing plugin before I assume there is no way for it to know what methods to call in the graphing plugin. I presume that in these cases, the developer of the data generator plugin must have access to a description of the graphing plugin API either in the form of an abstract class or an interface. Is this how plugin dependency operates, ie plugins know explicitly about the Apis that other plugins might have?
I've just built such a plugin system and for plugins to be able to use other plugins I am including in the source code copies of the plugin interfaces each plugin needs to know about. The problem with this approach is that if a new plotting plugin comes along but with a different API, there is no way for the data generator plugin to use it without first being recompiled so that it is aware of the new API. This doesn't seem right to me.
I know this may seem to be a very simple question and have an obvious answer but I've spend hours searching the internet and I've not come across an explicit statement concerning this question.
If your "new plotting plugin" has a different API from the one your code knows about, there is no alternative but to make your code aware of this API.
If you are in control of all this, including the various plotting plugins, then you c/should specify a standard Plotting API that all plotting plugins need to implement/support. That is about the only way that you can have different providers (plugins) for some task.
A standard "language" is the way to ensure that you can use multiple implementors of an interface (providers of a service). It is also the way that you can have multiple users of the same interface (consumers of a service).
The need/wish for multiple providers of a task and for multiple consumers of a provider is probably what led to the creation of standards such as OAuth, and of protocols such as HTTP, SMTP and the like.

Repository pattern for use in Reports only?

I am creating an MVC application where I'm going to be creating quite a few RDLC based reports. Looking at some of the examples out there on the web, it seems like the best way to use the report data source is through a repository.
My application will have to work with several databases. I already use the pattern to read/write to the database through POCO objects.
1) Rather than extend my current repository classes which are in my domain project, I was going to add separate repository classes to my Web project that deals only with the reports requirements.
2) Would it make sense to use the Repository pattern to only return different IQueryable/IEnumerable result sets that I could then use within my reports? Or would this be the wrong use of the REpository pattern?
I'd really like to hear your views on how you would go about with the above.
Thanks
It's better to use queries for reports because you need nothing but "read" operations there. Take a look at CQS(CQRS) pattern.

Making validation part of application settings

I am looking for examples of configuration based validation using Validation Application Block. I've found this
I want to ask if someone has an alternative solution to using EL VAB 5.0 to achieve configuration based validation. I've started with DataAnnotations but soon found out that some properties will need different types of validation depending on who is using the application.
Also if anyone has more examples for configuration with VAB and any advice as to what I might run into, please share.
There are several paths you can walk to achieve this. First of all you can (ab)use rulesets for this. You can create a 'base' ruleset with rules that hold for everybody and you can make a ruleset per role in the system and perhaps even a ruleset per user, but of course that would be cumbersome.
Another option would be to create an IConfigurationSource implementation that is able to return a ValidationSettings instance, based on the logged in user. Now there are several ways that you can build a ValidationSettings object. Here are a few examples:
You can load multiple configuration files from disk using the FileConfigurationSource based on the role. Something like: return (new FileConfigurationSource('validation_' + role + '.config')).GetSection(sectionName);
You can build up the ValidationSettings instances dynamically (and cache them). You can store this definition in the database and load them (this would be a lot of work) or define them in code (perhaps separated by assembly). Here is an example of a code based configuration.
Also to prevent having to duplicate parts of your configuration, you can do the following:
Merge multiple configurations together. You can for instance merge the base line validation with the role specific validation. This saves you from having to manually validate according to the base line and do a second validation for the role specific validation. While this is not supported out of the box, I wrote about how to do this on my blog here.
You can merge rules based on type inheritance. While VAB only supports validator inheritance for attribute based validation out of the box, I've wrote about this on my blog, here.
I hope this helps.

Grails build-test-data

Is it advisable to use the build-test-data plugin to load the bootstrap (seed/initial) data for an application. The plugin tutorial is excellent at http://bitbucket.org/tednaleid/grails-test-data/wiki/Home , but only mention about loading test data. There is a section about TestDataConfig , which allows to set default data. But is it a viable option if the data needs to persist in a larger scale, with complex relations.\
thanks.
Testing data is the primary focus of the plugin, but I use it for all kinds of data loading situations, including bootstrapping data into a new system.
The only thing you need to be aware of is that the plugin, by design, will fill in any holes in required data that you don't supply. This means that you should specify everything that you actually want specific values on (or putting it in the TestDataConfig that you mention). If you don't give build-test-data a value, it'll make something up and that might be something that you don't want.
The newly added functionality around buildLazy makes it even easier to hook into an existing graph of objects that you might have in a BootStrap configuration.
It depends on your data. If you only need a few things like administrator account info, list of categories, etc it should work fine. I'd use it if you’re testing your app with the initial seed data and everything works fine. For large or complex data imports I'd use a gant script to create and save for all the domain objects. For example I'm working on a project that requires me to move data out of a legacy database into a grails application. For this I run a script that uses JDBC calls to get all the old data out of the legacy database. I then create and save new domain objects based off this data. For an example of how to run a script that has access to the entire Grails context including Gorm see this

Resources