4

I have a class, which has a default constructor inherently.

public class OneRollingFileAppender : RollingFileAppender
{
    #region RollingFileAppender Overrides

    protected override void Append(LoggingEvent loggingEvent)
    {

        GlobalFactory<ILoggingEventParameterManager>.Instance.Apply(loggingEvent);
        base.Append(loggingEvent);
    }

    #endregion
}

Without editing the code, e.g adding a new constructor or property, how can I breakpoint default constructor?

NOTE: There should be a technique which will to find the code in IL or in memory, and then I'd like to set a breakpoint there.

johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • 2
    I don't think you can if you don't want to explicitly define a constructor – samyap Aug 01 '19 at 18:25
  • 1
    You can't set a breakpoint in code that doesn't exist. –  Aug 01 '19 at 18:26
  • @Amy, that code exists in IL, additionally there should be a spot in memory I can break point – johnny 5 Aug 01 '19 at 18:41
  • @johnny5 I'm aware, but you can't set a breakpoint in **C# code** if that code does not exist. –  Aug 01 '19 at 18:43
  • Maybe this: https://stackoverflow.com/questions/2617659/how-can-i-set-a-breakpoint-in-referenced-code-in-visual-studio – MikeH Aug 01 '19 at 18:44
  • @Amy, I want to set a breakpoint in visual studio, I don't care if its in the C# code, or in Memory or in IL. I only mentioned that I'm using c# for additional context because other languages (like c++) provide support – johnny 5 Aug 01 '19 at 18:46
  • @MikeH, I was trying that, I couldn't get the breakpoint to hit, maybe it's because its not a normal function – johnny 5 Aug 01 '19 at 18:49
  • Why do you want to set the breakpoint? – Postlagerkarte Aug 01 '19 at 18:54
  • @Postlagerkarte I want to see when a new class of this type is created – johnny 5 Aug 01 '19 at 19:00
  • @johnny5 In that case I suggest you use a named function breakpoint - if you have more debugging needs use my other answer. – Postlagerkarte Aug 01 '19 at 19:02
  • @Amy, this class has to be at some point in the code. There are lines of code in assembly which represent the default constructor, Why do you think this is not possible? – johnny 5 Aug 01 '19 at 19:04
  • @johnny5 Because I've tried to do this before. You can't set a breakpoint in IL. You keep replying with the suggestion that I don't understand what you're trying to do, and I keep replying that yes, I do. –  Aug 01 '19 at 19:07
  • Turns out all you have to do is break on function name, and pass the name of the constructor as the function – johnny 5 Aug 02 '19 at 14:21

4 Answers4

4

Use WinDbg + SOS Extension

  1. Attach to the process with Windbg (File/Attach to process)

  2. load sos (.loadby sos mscorwks)

  3. Set the breakpoint ( !bpmd mylib.dll Namespace.ClassName..ctor )

If you just want to know when the class is created you can make use of a function breakpoint within Visual Studio. Debug -> New Breakpoint

enter image description here

As Function name you enter your class name.

Community
  • 1
  • 1
Postlagerkarte
  • 6,600
  • 5
  • 33
  • 52
2

If you create a variable and assign a value in your class you can place a break point there which will be hit when an instance of the class is created.

public class OneRollingFileAppender : RollingFileAppender
{
  int foo = 0; //Place break point here
}
MikeH
  • 4,242
  • 1
  • 17
  • 32
0

I believe you are using log4net. As you don't have source code so you cannot put break point on your constructor.

0

You can't put a breakpoint in compiled code that you don't control. However, with something like ReSharper, you can step into third party code. See: https://www.jetbrains.com/help/resharper/Debugging_Without_Source_Code.html

Jonathan
  • 4,916
  • 2
  • 20
  • 37