destructurama / attributed

Use attributes to control how complex types are logged to Serilog.
Apache License 2.0
269 stars 33 forks source link

Add support for MetadataTypeAttribute #40

Closed daniel-white closed 6 months ago

daniel-white commented 4 years ago

System.ComponentModel.DataAnnotations.MetadataTypeAttribute is a handy bridge to attributes defined in other files, especially in code generated files. It works with the validation frameworks. I don't see why it wouldn't here.

Example:

[GeneratedCode]
[MetadataTypeAttribute(typeof(DtoMetadata))]
public partial class Dto
{
    public string Private { get; set; }
}

internal class DtoMetadata
{
     [NotLogged]
     public object Private { get; set; }
}

I think it would be great if both sources could be utilized, but the configuration on the primary class would win in the event of a conflict.

sungam3r commented 4 years ago

What problem do you solve?

daniel-white commented 4 years ago

What problem do you solve?

It solves one case where the properties are defined in a code generated class file. One wouldn’t want to put the attributes in there as they’d get overwritten by the generator

sungam3r commented 4 years ago

ok

daniel-white commented 4 years ago

@nblumhardt is this something you would take a PR for? I'd be happy to start looking how to add this.

I've found this as a potential option: https://stackoverflow.com/questions/17873576/metadata-were-not-loaded-using-metadatatype

nblumhardt commented 4 years ago

Hi @daniel-white - I think so :+1:

daniel-white commented 4 years ago

doh. the attribute isn't available in .net standard, so i could add one to the library and reflect on that or just wait till everything is .net 5.

nblumhardt commented 4 years ago

Targeting netcoreapp3.0 or 3.1 would also be fine 👍

digitalsigi commented 3 years ago

I am scaffolding repeatedly a database structure and separated all attributes in a separate file. So I have exactly the same problem.

@nblumhardt May be we can somehow work together to get it done. I am not experienced in multi-targeting, so I would need some help here.

According to Micosoft it sounds to go straight to net5.0.

digitalsigi commented 3 years ago

So was digging a litte deeper into this.

@daniel-white is right, metadatatype comes from Net Framework and is available in net core 3.x but not in net standard. So I will proceed with net5.0

SimonCropp commented 3 years ago

@digitalsigi are you still interested in submitting a PR for this?

digitalsigi commented 3 years ago

Unfortunately not for the moment. I am still busy with a larger project. Had a quick look into the code, it looks feasable, but its more than just hack an go. If it helps, I could provide some code snippets showing how to dig into attributes hierarchies.

github-actions[bot] commented 9 months ago

This issue was marked as stale since it has not been active for a long time

digitalsigi commented 9 months ago

OK, got a ping from github. Here a code snippet I am using to get all properties which are then filtered to select only those with my Custom Attribute:

           var propBaseType = vmType.GetProperties().ToList();
           var metaType = vmType.GetCustomAttributes<MetadataTypeAttribute>(true).ToList().FirstOrDefault();
           var metaProp = new List<PropertyInfo>();
           if (metaType != null)
           {
               var metaClass = metaType.MetadataClassType;
               metaProp = metaClass.GetProperties().ToList();
           }

           var propConcat = metaProp.Concat(propBaseType) // Concatenate properties of MetaData class and Base class
                                                          // Take only first -> the one of Metadata, assumed metadata has the avs attribute
                                                          // if base class bears an avs attibute it is silently ignored
                                   .OrderBy(m => m.Name)
                                   ;

I guess something like this has to be added in GetPropertiesRecursive, but I still can't find the time to do it.

sungam3r commented 9 months ago

It definetely can be added. The main thing here is to match attributes from other type to the properties of original type.

sungam3r commented 9 months ago

If someone wants to work on this issue, I will be grateful.

sungam3r commented 9 months ago

I don't think I will work on this feature. Anyone can start up from #91.

sungam3r commented 9 months ago

ping @daniel-white @digitalsigi

github-actions[bot] commented 8 months ago

This issue was marked as stale since it has not been active for a long time