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

Fix crash on apps built with PublishTrimmed=True #110

Merged
merged 1 commit into from
Jan 7, 2024

Conversation

malciin
Copy link
Contributor

@malciin malciin commented Jan 5, 2024

I've found that Serilog.Expressions produces nasty startup crash when an app that uses this lib is built with PublishTrimmed=true:

Unhandled exception. System.ArgumentException: The function name `Substring` was not recognized.
   at Serilog.Expressions.Compilation.Linq.LinqExpressionCompiler.Transform(CallExpression)
   at Serilog.Expressions.Compilation.Transformations.SerilogExpressionTransformer`1.Transform(Expression expression)
   at Serilog.Expressions.Compilation.Linq.LinqExpressionCompiler.Compile(Expression, IFormatProvider , NameResolver)
   at Serilog.Expressions.Compilation.ExpressionCompiler.Compile(Expression, IFormatProvider , NameResolver)
   at Serilog.Expressions.SerilogExpression.TryCompileImpl(String, IFormatProvider , NameResolver , CompiledExpression& , String& )
   at Serilog.Expressions.SerilogExpression.Compile(String, IFormatProvider , NameResolver )
   at Serilog.LoggerEnrichmentConfigurationExtensions.WithComputed(LoggerEnrichmentConfiguration, String, String)
   at Program.<>c.<<Main>$>b__0_1(HostBuilderContext _, LoggerConfiguration configuration) in D:\serilog-expressions-substring-error\Program.cs:line 7
   at Serilog.SerilogHostBuilderExtensions.<>c__DisplayClass1_0.<UseSerilog>b__0(HostBuilderContext hostBuilderContext, IServiceProvider services, LoggerConfiguration loggerConfiguration)
   at Serilog.SerilogHostBuilderExtensions.<>c__DisplayClass2_1.<UseSerilog>b__1(IServiceProvider services, LoggerConfiguration loggerConfiguration)
   at Serilog.SerilogServiceCollectionExtensions.<>c__DisplayClass3_0.<AddSerilog>b__0(IServiceProvider services)
   // rest stack unrelevant

Reproduction repo: https://github.com/malciin/serilog-expressions-trim-crash

I've investigated it and found that's because _nameResolver does not have any operator registered in it at all because those operators are trimmed away.

throw new ArgumentException($"The function name `{call.OperatorName}` was not recognized.");

Adding [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] to StaticMemberNameResolver fixes it.

Repo with fixed app: https://github.com/malciin/serilog-expressions/tree/dev-trimmed-test-app/src/SerilogTrimTestApp

@nblumhardt
Copy link
Member

Excellent, thank you.

@nblumhardt nblumhardt merged commit 6c9f2f5 into serilog:dev Jan 7, 2024
1 check passed
@nblumhardt nblumhardt mentioned this pull request Jan 7, 2024
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.

2 participants