Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support of constructor parameters #281

Merged
merged 2 commits into from
Aug 31, 2021
Merged

Conversation

skomis-mm
Copy link
Contributor

@skomis-mm skomis-mm commented Aug 30, 2021

Added support for constructor parameters (implementation of second design) including nested configurations and type binding that StringArgumentValue supports.

"formatter": {
  "type": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
  "valueFormatter": {
    "typeTagName": "customTypeTag"
  }
},

"formatter": {
    "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
    "template": "[{@t:HH:mm:ss} {@l:u3} {Coalesce(SourceContext, '<none>')}] {@m}\n{@x}",
    "formatProvider": "System.Globalization.CultureInfo::InvariantCulture"
}
  1. Prefix $ for type is optional to go in line with level/filter swithes declaration syntax
  2. Ctor overload matching follows the same algorithm that is used in configuration method matching

Copy link
Member

@nblumhardt nblumhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just had one minor query; looks good to me. Seems like it would be wise for us to queue up a 3.3.0 release shortly after merging - the world needs this! :-D

README.md Outdated Show resolved Hide resolved
@nblumhardt nblumhardt merged commit 10a2cbd into serilog:dev Aug 31, 2021
@nblumhardt nblumhardt mentioned this pull request Aug 31, 2021
@skomis-mm skomis-mm deleted the ctorArgs branch August 31, 2021 05:34
@guilhermemalfatti
Copy link

awesome!!!

@WattsC-90
Copy link

WattsC-90 commented Aug 19, 2022

I apologise given this is an old PR, but how do you pass a constructor argument using config?

I am trying to pass the renderMessage:true argument to the inbuilt JsonFormatter, but cannot see given the example above how to do this (nor by reading the linked issue).

If i try what seems indicated:

"formatter": {
    "type": "Serilog.Formatting.Json.JsonFormatter",
    "renderMessage": true
}

I get:
image

@wertzui
Copy link

wertzui commented Sep 12, 2022

I've got the same or a simmilar problem as @WattsC-90

This is in my appsettings.json

"Name": "Console",
"Args": {
  "formatter": {
    "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
    "template": "[{@t:HH:mm:ss} {@l:u3} {Coalesce(SourceContext, '<none>')}] {@m}\n{@x}",
    "formatProvider": "System.Globalization.CultureInfo::InvariantCulture"
  }
}

And this is the exception

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.InvalidOperationException: Cannot create instance of type 'Serilog.Formatting.ITextFormatter' because it is either abstract or an interface.
   at Microsoft.Extensions.Configuration.ConfigurationBinder.CreateInstance(Type type)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config, BinderOptions options)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.Get(IConfiguration configuration, Type type, Action`1 configureOptions)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.Get(IConfiguration configuration, Type type)
   at Serilog.Settings.Configuration.ObjectArgumentValue.ConvertTo(Type toType, ResolutionContext resolutionContext)
   at Serilog.Settings.Configuration.ConfigurationReader.<>c__DisplayClass21_2.<CallConfigurationMethods>b__3(<>f__AnonymousType9`2 <>h__TransparentIdentifier0)
   at System.Linq.Utilities.<>c__DisplayClass2_0`3.<CombineSelectors>b__0(TSource x)
   at System.Linq.Enumerable.SelectListPartitionIterator`2.ToList()
   at Serilog.Settings.Configuration.ConfigurationReader.CallConfigurationMethods(ILookup`2 methods, IList`1 configurationMethods, Object receiver)
   at Serilog.Configuration.LoggerSinkConfiguration.Wrap(LoggerSinkConfiguration loggerSinkConfiguration, Func`2 wrapSink, Action`1 configureWrappedSink, LogEventLevel restrictedToMinimumLevel, LoggingLevelSwitch levelSwitch)
   at Serilog.LoggerConfigurationAsyncExtensions.Async(LoggerSinkConfiguration loggerSinkConfiguration, Action`1 configure, Int32 bufferSize, Boolean blockWhenFull)

@sergiuciudin
Copy link

@wertzui @skomis-mm
Same for me when using type and template. Works with JSON formatter.

@jmrtnz94
Copy link

jmrtnz94 commented Aug 18, 2023

I apologise given this is an old PR, but how do you pass a constructor argument using config?

I am trying to pass the renderMessage:true argument to the inbuilt JsonFormatter, but cannot see given the example above how to do this (nor by reading the linked issue).

If i try what seems indicated:

"formatter": {
    "type": "Serilog.Formatting.Json.JsonFormatter",
    "renderMessage": true
}

I get: image

I know this is a late reply, but I got it working with the following code. The difference is including "Serilog" in the type which I'm assuming is the name of the Nuget Pacakge?

"WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "formatter": {
        "type": "Serilog.Formatting.Json.JsonFormatter, Serilog",
        "renderMessage": true
      }
    }
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Serilog expressions in sink configuration
7 participants