Is it normal that to register an entity framework you need to debug a window in the product source code?

Can this line be used in the source code of the product? What are the performance penalties? I know that Debug.WriteLine is not thrown to the assembly built with the release configuration.

ddiCatalogEntities.Database.Log = msg => Debug.WriteLine(msg); 

This line should log the output of the Entity structure log in the Visual Studio Debug Console while in debug mode.

 #if DEBUG ddiCatalogEntities.Database.Log = msg => Debug.WriteLine(msg); #endif 

Would you prefer the solution above?

+5
source share
2 answers

First, note that you cannot specify a normal delegate with the ConditionalAttribute method.

However, you use lambda to compile OK. But what does he actually compile?

Consider this code:

 Action<string> print = message => Debug.WriteLine(message); print("TEST"); 

To build debugging, this compiles to:

 Action<string> print = delegate (string message) { Debug.WriteLine(message); }; print("TEST"); 

To build the release, it compiles to:

 Action<string> print = delegate (string message) { }; print("TEST"); 

In both cases, a delegate is created and called - so in both cases you will have the overhead of creating and calling a delegate (this parameter is pushed onto the stack), although the release version actually does nothing.

So, for your case, the difference between using #if DEBUG or not is as follows:

  • If you use #if DEBUG , then there is no overhead, and the Log property will not be set.
  • Otherwise, you have overhead when setting up the Log property, and then it does nothing when it is called.

In many cases, the overhead is so small that you don't mind - and it's also pretty nice to ensure that the Log property is always set to something (the delegate doesnโ€™t do anything by default) t you need to check it against zero before referencing on him every time.

Using #if DEBUG , on the other hand, makes things more clear. The way lambda interacts with the method defined with ConditionalAttribute is not completely obvious!

All this is a roundabout way of saying: weigh the pros and cons and make your choice .;)

+6
source

Members of the Debug class are marked with a conditional attribute, so if you use the Release configuration, they will not compile.

+1
source

Source: https://habr.com/ru/post/1200736/


All Articles