how to get all previous called methods from first to end methods - c#

Let's say we have this code
namespace app.Entities
{
public class school
{
public bool Addschool() { }
}
}
namespace app.layer1
{
public class ManageSchool
{
public bool schoolInfo() {
schoolInfo.addSchool();
}
}
}
namespace app.layer
{
public class schoolAPi
{
public bool GetAndAdd()
{
ManageSchool.schoolInfo();
}
}
}
i want to know which layers and fuctions called AddSchool() method in app.entitied namespace
for exp :
app.layer2.schoolAPi.GetAndAdd >> app.layer1.ManageSchool.schoolInfo >> app.Entities.school.Addschool

Try look at System.Diagnostics.StackTrace and System.Diagnostics.StackFrame classes.
You can create some Tracer class with Trace method to trace each call, but you supposed to put trace in each method to be able to trace it.
Example of Tracer.cs:
using System.Diagnostics;
public class Tracer
{
public static void Trace()
{
StackFrame sf = new StackTrace(true).GetFrame(1);
Console.WriteLine("Called {0} in {1} at line: {2}", sf.GetMethod().ToString(), sf.GetFileName(), sf.GetFileLineNumber());
}
}
Examples of your project:
Entities.cs:
namespace App.Entities
{
public class School
{
public static bool AddSchool()
{
Tracer.Trace();
return true;
}
}
}
LayerOne.cs:
using App.Entities;
namespace App.LayerOne
{
public class ManageSchool
{
public static bool SchoolInfo()
{
Tracer.Trace();
School.AddSchool();
return true;
}
}
}
LayerTwo.cs:
using App.LayerOne;
namespace App.LayerTwo
{
public class SchoolAPI
{
public static bool GetAndAdd()
{
Tracer.Trace();
ManageSchool.SchoolInfo();
return true;
}
}
}
And a Program.cs:
using System;
using App.LayerTwo;
namespace App
{
class Program
{
static void Main(string[] args)
{
Tracer.Trace();
SchoolMethod();
Console.ReadKey();
}
static void SchoolMethod()
{
Tracer.Trace();
SchoolAPI.GetAndAdd();
}
}
}
It will give you some kind of this result:
Also you can put Tracer.Trace() method under bool condition of some global setting and use it only when you need trace:
if (MySettingsClass.TraceEnabled)
Tracer.Trace();

Related

How to use common trace id?

I want to use common trace id. I'm using following code.
public void method1(){
using (new Tracer(Guid.NewGuid().ToString()))
{
//my code
}
}
public void method2(){
using (new Tracer(Guid.NewGuid().ToString()))
{
//my code
}
}
Here guid is my trace id. But different trace id generating for every method call. I want keep it as unique. How to achieve this?. (note : I call method1,method2 from some different client)
If you need to get info about class name and/or your .NET <= 4.0, use StackFrame. You'll get some overhead with StackFrame. If you don't need to get the name of class and you use .NET >= 4.5, here is solution. It uses Caller Information. :
namespace Tracer
{
using System;
using System.Runtime.CompilerServices;
sealed class CallerInfoTracer : IDisposable
{
private readonly string _message;
private readonly string _memberName;
private readonly string _sourceFilePath;
private readonly int _lineNumber;
private bool _disposed;
public CallerInfoTracer(string message, [CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int lineNumber = 0)
{
_message = message;
_memberName = memberName;
_sourceFilePath = sourceFilePath;
_lineNumber = lineNumber;
}
public void Dispose()
{
if (_disposed) return;
Console.WriteLine("Message: {0}", _message);
Console.WriteLine("MemberName: {0}", _memberName);
Console.WriteLine("SourceFilePath: {0}", _sourceFilePath);
Console.WriteLine("LineNumber: {0}", _lineNumber);
_disposed = true;
}
}
public class Program
{
public static void Main(string[] args)
{
Method1();
Method2();
}
public static void Method1()
{
using (var tracer = new CallerInfoTracer("Desc1")) { }
}
public static void Method2()
{
using (var tracer = new CallerInfoTracer("Desc2")) { }
}
}
}

Use a class in two different namespace

How do use a class from namespace in other namespace?
namespace All
{
class X
{
public static void Read() {}
}
class Y
{
public static void Write() {}
}
}
namespace A
{
namespace First{
//use class X in namespace All here;
}
}
namespace B
{
namespace Second{
//use class Y in namespace All here;
}
}
i don't want copy paste code,is there a keyword in C# to reference the hole the class? use it here like
B.Second.Y.Write();
you cannot directly use other types as variable in a namespace
So
You may use those types (X, Y) in the types in those namespaces (First, Second). e.g
namespace All
{
class X
{
public static void Read() { }
}
class Y
{
public static void Write() { }
}
}
namespace A
{
namespace First
{
//use class X in namespace All here;
}
}
namespace B
{
namespace Second
{
//use class Y in namespace All here;
class MyClass
{
private All.X; //HERE is the code
}
}
}
Try the using Keyword
using A;
using B;
using All;
If you define namespace A & B as followed:
namespace All
{
public class X
{
public static void Read() { }
}
public class Y
{
public static void Write() { }
}
}
namespace A
{
namespace First
{
public static class X
{
public static void Read()
{
All.X.Read();
}
}
}
}
namespace B
{
namespace Second
{
public static class Y
{
public static void Write()
{
All.Y.Write();
}
}
}
}
You can then use it like so...
namespace CodeResearch.StackOverFlowTests
{
using System;
/// <summary>
/// Defines DebugStackOverFlow type
/// </summary>
public class DebugStackOverFlow
{
/// <summary>
/// Tests debugging StackOverFlow questions
/// </summary>
public static void Test()
{
A.First.X.Read();
B.Second.Y.Write();
Console.ReadLine();
}
}
}
NOTE:
I have all of this defined in the same cs file.

How to call function inside custom attribute class

Lets say i have the following attribute class.
//Attribute Implementation
public abstract class TestAttribute : Attribute
{
public abstract void UpdateSomething(string s);
}
public class CustomAttTest : TestAttribute
{
private State state;
public CustomAttTest(State state)
{
this.state = state;
}
public override void UpdateSomething(string s)
{
if (state.Equals(State.First))
{
Console.WriteLine("First State!! " + s);
}
}
}
public enum State
{
First, Second, Third
}
How can i call the Updatesomthing function inside the attribute class?
following is the attribute implementation example.
public abstract class Vehicle
{
//Coode
}
[CustomAttTest(State.First)]
public class Ferrari : Vehicle
{
//Code
}
Here is the full code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
var foo = new Ferrari();
//How do i call the UpdateSomething implemented insde the CustomAttTest attribute class?
}
}
public abstract class Vehicle
{
//Coode
}
[CustomAttTest(State.First)]
public class Ferrari : Vehicle
{
//Code
}
//Attribute Implementation
public abstract class TestAttribute : Attribute
{
public abstract void UpdateSomething(string s);
}
public class CustomAttTest : TestAttribute
{
private State state;
public CustomAttTest(State state)
{
this.state = state;
}
public override void UpdateSomething(string s)
{
if (state.Equals(State.First))
{
Console.WriteLine("First State!! " + s);
}
}
}
public enum State
{
First, Second, Third
}
}
You need to use reflection:
foo.GetType().GetCustomAttribute<CustomAttTest>().UpdateSomething(...);
However, you should probably use an abstract method or property instead of an attribute.

Ensures Unproven via property when implementing interface

I'm trying what, to me, seems like some fairly basic code contracts code. I've reduced it down to the following problem. The following fails the static analysis, with the message
CodeContracts: ensures unproven:
this.Frozen
using System;
using System.Diagnostics.Contracts;
namespace PlayAreaCollection2010
{
public class StrippedContract : IBasic
{
private bool _frozen = false;
public void Freeze()
{
_frozen = true;
}
public bool Frozen { get { return _frozen; } }
}
[ContractClass(typeof(IBasicContract))]
public interface IBasic
{
void Freeze();
bool Frozen { get; }
}
[ContractClassFor(typeof(IBasic))]
public abstract class IBasicContract : IBasic
{
#region IBasic Members
public void Freeze()
{
Contract.Ensures(this.Frozen);
}
public bool Frozen
{
get { return true;}
}
#endregion
}
}
However, the following works fine and satisfies all checks:
using System;
using System.Diagnostics.Contracts;
namespace PlayAreaCollection2010
{
public class StrippedContract
{
private bool _frozen = false;
public void Freeze()
{
Contract.Ensures(this.Frozen);
_frozen = true;
}
public bool Frozen { get { return _frozen; } }
}
}
CodeContracts: Checked 1 assertion: 1 correct
What do I need to do to satisfy the static checker, when I've placed my contracts in the interface?
In your implementation of IBasic, StrippedContract, you will need to add a post-condition to the Frozen property:
public bool Frozen {
get {
Contract.Ensures(Contract.Result<bool>() == this._frozen);
return _frozen;
}
}
Alternatively, you could add the -infer command line option to the static checker in the Code Contracts tab of your project's properties. That will allow the static checker to infer this automatically.

ToString() does not return the expected string

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3Generics
{
class Program
{
static void Main(string[] args)
{
ScheduleSelectedItems sitems = new ScheduleSelectedItems("Yusuf");
ScheduleSelectedItemsList slist = new ScheduleSelectedItemsList();
slist.Items.Add(sitems);
Console.Write(slist.Items[0].ToString());
Console.ReadKey();
}
}
public class ScheduleSelectedItems
{
private string Ad;
public ScheduleSelectedItems(string ad)
{
Ad = ad;
}
}
public class ScheduleSelectedItemsList
{
public List Items;
public ScheduleSelectedItemsList()
{
Items = new List();
}
}
}
how can i add "yusuf" on my Console?
public class ScheduleSelectedItems
{
private string Ad;
public ScheduleSelectedItems(string ad)
{
Ad = ad;
}
public override string ToString()
{
return this.Ad;
}
}
What BFree said, with a slight modification to make it singular instead of plural:
public class ScheduleSelectedItem
{
private string Ad;
public ScheduleSelectedItem(string ad)
{
Ad = ad;
}
public override string ToString()
{
return this.Ad;
}
}
Additionally, you want an "Add" method for your list. While you're at it, why not just inherit from the list class:
public class ScheduleSelectedItemsList : List<ScheduleSelectedItem>
{
}
Or you could just create a type alias:
using ScheduleSelectedItemsList = List<ScheduleSelectedItem>;
Either way, you can use the new code like this:
class Program
{
static void Main(string[] args)
{
var slist = new ScheduleSelectedItemsList()
{
new ScheduleSelectedItem("Yusuf")
};
//write every item to the console, not just the first
slist.All(item => Console.Write(item.ToString()) );
Console.ReadKey();
}
}
Add this to your ScheduleSelectedItems class:
public override string ToString() {
return Ad;
}
That tells the system how such an object should be formatted.
You need to override the toString() method of ScheduleSelectedItems to return 'Ad'.

Categories

Resources