|
Hi Bruce,
the code shown is perfect. However the problem is elsewhere. You have to help those threads to find and use your log method. Especially for logging purposes I tend to do that with static events, like so
(incomplete and unchecked code):
class Form1 : Form {
public Form() {
...
Class2.Logger+=log;
}
public void log(string s) {...}
}
and the "log clients" look like this:
class Class2 {
public static event Action<string> Logger;
private void log(string s) {
if (Logger!=null) Logger(s);
}
}
BTW: the event does not have to be static, but it allows for doing Logger+=log before the actual client objects have been created.
PS: I always like to have a log(Exception) method too; I either solve that by doing the above stuff twice, or by adding the following code in every log client class:
private void log(Exception exc) {
foreach(string s in exc.ToString().Split('\r','\n')) if (s.Length!=0) log(s);
}
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Don't want to hijack the thread but...
I experimented briefly with static events a while ago and came across a slight issue of objects not getting disposed.
Normally, if a ClassA instance subscribes to an event in a ClassB instance, when ClassB instance goes out of scope, it will allow ClassA instance to be disposed if there are no other references. With a static event in ClassB, that never happens of course and ClassA instance will continue to exist for the application's lifetime unless it explicitly unsubscribes from the static event.
Worth bearing in mind as otherwise unexpected things can happen, as well as potential memory pressures.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi Dave,
IMO your remark is perfectly valid in general; however in this situation the form implementing the log functionality is assumed to be the main form, and to live for as long as the app is running. If not, one should consider implementing the log server in a separate class, or drop the statics.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
in my app i writes to eventlog , but in some machines t get an exception (unhandled) saying that cannot write to eventlog, i think it is because of some privileges , i want to fix this how can i fx it, and how can i set the privileges programatically
|
|
|
|
|
Hi I'm currently trying to dynamicly invoke each class, in a namespace. So far I have managed to take one class and illiterate through it's methods, properties, etc.. and return a collection of data that gets added to a dataset. However the adjacent classes in that namespace I also need to invoke dynamicly,
So far i've attempted this with Reflection as I did with the individual class, but at a namespace leave it seams I need a different approach
For the Record:
VS 2005 C# .NET 2.0
|
|
|
|
|
hey all,
I created a shared addin project in visual studio 2005, an addin to excel that creates a button on the excel standard bar and assigns some event to it. Also, my addin acts an Automation addin. Now when I built the 'Setup project' of my addin, I got a setup file which I installed on a test machine. On this test machine, to my surprise, the addin's automation part is not working but the COM part is working fine i.e., the button is coming up on the standard bar.
The reason I found out for this is, the Programmable key word is not being added to the HKCR\CLSID\GUID\ of my addin in the test machine.
How to resolve this problem!? I want excel to recognise the UDFs i defined in my addin so that it also acts as an automation addin.
Regards.
|
|
|
|
|
hello!
guys... isn't there anybody to discuss the problem and come up with atleast ideas to approach the solution?!
regards.
|
|
|
|
|
Hi,
I need to create a session level connection object. The situation in like that, there can be multiple users loging in simultaneously. I need a different connection object for each user and want to maintain the count of the connection objects.
Platform: ASP.Net,C# 2005
Please help.
regards,
Abhishek agrawal
abhishek
|
|
|
|
|
i know some english. i hope you understand me.
i developing a windows application with visual studio 2005.
i try changing comboBox font name in visual studio properties window. but font name not view all windows fonts(windows/fonts).
how to view all fonts in windows/fonts directory.
best regards
Tayfun ISTANBUL
|
|
|
|
|
The Font dropdown in the properties window should show all installed fonts.
It is possible to have font files in the Fonts Directory that have not been installed.
So firstly, make sure that the font you are looking for has been installed. (Open the Fonts Directory in Explorer and right-click, just in case you don't know.)
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
How to do unit testing to non-function? I'm using vsts 2008 and i able to create a unit testing for function but i'm not sure how to create for sequential structure code.
plz help!
modified on Tuesday, April 7, 2009 10:16 PM
|
|
|
|
|
I have a list that consists of 'customer'-objects. Every 'customer' has a full name, address, email, and phone. Now I want to be able to do a search that lets the user type in information in a form and then perform a search based on the information the user typed in. In the form the user can type in 'first name', 'last name', 'address' and so on.
But how do I compare the information the user types in with the information stored in the 'customer' objects? If the user only types in the 'first name' and 'phone' for example the search must pick out all 'customers' that matches that.
Thanks for help!
|
|
|
|
|
So, you go through all your objects, and you match all with the same first name, and phone, if that's what they typed.
Christian Graus
Driven to the arms of OSX by Vista.
"I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
|
|
|
|
|
Maybe you can modify this a bit to suit your own needs. I am searching on one given string in a text box and pull all data in the DB to a listbox.
private void btnSearch_Click(object sender, EventArgs e)
{
FillListBox(textBox1.Text);
}
public void FillListBox(string searchValue)
{
try
{
dataTable.Clear();
DataAdapter =
new OleDbDataAdapter
("Select [PicId], [Path] From [Pics] WHERE PicId = '" +
searchValue + "'", conn);
DataAdapter.Fill(dataTable);
listBox1.DataSource = dataTable;
listBox1.DisplayMember = "Path";
listBox1.ValueMember = "Path";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
Thanks for your replies.
I came up with a solution that worked well but I don't know if it's optimal:
I search through all the information stored in each customer in the list. If the information in the customer object matches the information the user typed in the textbox the result is stored in a new list. If the information in the search textbox is empty it's also seen as a match. See below:
public int[] SearchCustomers(Customer searchCustomer)
{
//Create list for search results.
searchResult = new List<Customer>();
//Loop through all information in the customer-objects.
searchResult = customers.FindAll(delegate(Customer C) { return SearchFirstName(C, searchCustomer); });
searchResult = searchResult.FindAll(delegate(Customer C) { return SearchLastName(C, searchCustomer); });
searchResult = searchResult.FindAll(delegate(Customer C) { return SearchCity(C, searchCustomer); });
searchResult = searchResult.FindAll(delegate(Customer C) { return SearchCountry(C, searchCustomer); });
searchResult = searchResult.FindAll(delegate(Customer C) { return SearchStreet(C, searchCustomer); });
searchResult = searchResult.FindAll(delegate(Customer C) { return SearchZip(C, searchCustomer); });
searchResult = searchResult.FindAll(delegate(Customer C) { return SearchCellPhone(C, searchCustomer); });
searchResult = searchResult.FindAll(delegate(Customer C) { return SearchHomePhone(C, searchCustomer); });
searchResult = searchResult.FindAll(delegate(Customer C) { return SearchEmail(C, searchCustomer); });
The final result is a list containing the matches of all search words.
|
|
|
|
|
Hi,
How can i compress and extract a folder programmatically?
Can i get any sample source code?
Thankyou,
YPKI
|
|
|
|
|
You can use J# for that, or you need a third party component.
Christian Graus
Driven to the arms of OSX by Vista.
"I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
|
|
|
|
|
Why are you recommending J#?
|
|
|
|
|
You can Use GZipStream and DeflateStream classes from namespace System.IO.Compression
*12Code
|
|
|
|
|
You can Use GZipStream and DeflateStream classes from namespace System.IO.Compression
Hope this can help.
*12Code
|
|
|
|
|
|
|
hello
This scripts runs okay on SQL*Plus and TOAD (using same account "dev") but not sure when I try to wire it down through "System.Data.OracleClient" I keep getting the following error:
<br />
ORA-06550: line 1, column 8:\nPLS-00103: Encountered the symbol \"\" when expecting one of the following:\n\n begin function package pragma procedure subtype type use\n ... System.Data.OracleClient.OracleException...<br />
Here's the code:
<br />
DECLARE<br />
count_item int;<br />
BEGIN<br />
SELECT count(1) into count_item FROM user_sequences WHERE sequence_name = 'AUDITLOGSEQUENCE';<br />
IF count_item > 0 THEN<br />
begin<br />
dbms_output.put_line('drop sequence AUDITLOGSEQUENCE');<br />
EXECUTE IMMEDIATE ('DROP SEQUENCE AUDITLOGSEQUENCE');<br />
end;<br />
ELSE<br />
dbms_output.put_line('no need to drop AUDITLOGSEQUENCE');<br />
END IF;<br />
<br />
EXECUTE IMMEDIATE 'CREATE SEQUENCE AUDITLOGSEQUENCE<br />
MINVALUE 1<br />
MAXVALUE 999999999999999999999999999<br />
START WITH 1<br />
INCREMENT BY 1<br />
CACHE 20';<br />
dbms_output.put_line('AUDITLOGSEQUENCE created');<br />
<br />
SELECT count(1) into count_item FROM user_tables WHERE table_name = 'LOG';<br />
IF count_item > 0 THEN<br />
begin<br />
dbms_output.put_line('drop table LOG');<br />
EXECUTE IMMEDIATE ('DROP TABLE LOG');<br />
end;<br />
ELSE<br />
dbms_output.put_line('no need to drop table LOG');<br />
END IF;<br />
<br />
EXECUTE IMMEDIATE '<br />
CREATE TABLE LOG (<br />
Id numeric(19,0) NOT NULL,<br />
CreateDate date default sysdate NOT NULL,<br />
Thread varchar (255) NOT NULL,<br />
LogLevel varchar (50) NOT NULL,<br />
Logger varchar (255) NOT NULL,<br />
Message varchar (4000) NOT NULL,<br />
InnerException varchar (2000) NULL,<br />
CONSTRAINT PK_LOG PRIMARY KEY (Id)<br />
)';<br />
<br />
COMMIT;<br />
<br />
dbms_output.put_line('table LOG created');<br />
<br />
dbms_output.put_line('setup complete');<br />
EXCEPTION<br />
WHEN OTHERS THEN<br />
dbms_output.put_line('*** setup exception detected! ***');<br />
dbms_output.put_line('error code: ' || sqlcode);<br />
dbms_output.put_line('stack trace: ' || dbms_utility.format_error_backtrace);<br />
RAISE_APPLICATION_ERROR(-20000, 'AuditTrail.oracle.tables.sql - install failed');<br />
END;<br />
As you can see, the script is anonymous block taking no argument
And I looked at the code, can't see anything wrong.
<br />
oCmd = oConn.CreateCommand();<br />
oCmd.CommandText = DBUtil.FixParameterNameForOracle(oContext.DefaultDBProvider, StringUtil.ReplaceSpecialTokens(SQL));<br />
oCmd.CommandType = System.Data.CommandType.Text;<br />
<br />
oDataSet = new DataSet(CmdFilePath);<br />
oTable = new DataTable(Name);<br />
oDataSet.Tables.Add(oTable);<br />
oAdapter = oFactory.CreateDataAdapter();<br />
<br />
oAdapter.SelectCommand = (DbCommand)oCmd;<br />
oAdapter.Fill(oDataSet, Name);<br />
oCmd is of type "System.Data.OracleClient.OracleCommand". And it takes no parameter or input argument. I googled a bit closest I found is this:
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/579b5a98-ec74-4f6f-b5b4-6919d8bc3e7c/[^]
But my script contains no parameter.
Also, using "oCmd.ExecuteNonQuery()" instead of dataAdapter.Fill didn't help. I finally found one get around (NOT A DECENT SOLUTION!), that is to collapse the multiline script to single line!
<br />
DECLARE count_item int; BEGIN SELECT count(1) into count_item FROM user_sequences WHERE sequence_name = 'AUDITLOGSEQUENCE'; IF count_item > 0 THEN begin dbms_output.put_line('drop sequence AUDITLOGSEQUENCE'); EXECUTE IMMEDIATE ('DROP SEQUENCE AUDITLOGSEQUENCE'); end; ELSE dbms_output.put_line('no need to drop AUDITLOGSEQUENCE'); END IF; EXECUTE IMMEDIATE 'CREATE SEQUENCE AUDITLOGSEQUENCE MINVALUE 1 MAXVALUE 999999999999999999999999999 START WITH 1 INCREMENT BY 1 CACHE 20'; dbms_output.put_line('AUDITLOGSEQUENCE created'); SELECT count(1) into count_item FROM user_tables WHERE table_name = 'LOG'; IF count_item > 0 THEN begin dbms_output.put_line('drop table LOG'); EXECUTE IMMEDIATE ('DROP TABLE LOG'); end; ELSE dbms_output.put_line('no need to drop table LOG'); END IF; EXECUTE IMMEDIATE 'CREATE TABLE LOG (Id numeric(19,0) NOT NULL, CreateDate date default sysdate NOT NULL, Thread varchar (255) NOT NULL, LogLevel varchar (50) NOT NULL, Logger varchar (255) NOT NULL, Message varchar (4000) NOT NULL, InnerException varchar (2000) NULL,CONSTRAINT PK_LOG PRIMARY KEY (Id))'; dbms_output.put_line('table LOG created'); dbms_output.put_line('setup complete'); END;<br />
What the hell!? I have a big script next and I'm really not impressed ... any suggestion besides this?
Thanks
dev
modified on Tuesday, April 7, 2009 6:45 AM
|
|
|
|
|
found it... I fixed strSQL from application, removing all instances of newline
strSQL = strSQL.Replace('\n', ' ');
I'm very impressed by the work on System.Data.OracleClient, some quality code!
dev
|
|
|
|
|
I tried to list drives available in a system in checkedlistbox.When i click on the C:\ i want to list its files in a listview.I tried with the following but it does not work.Can you give me an alternative.<pre>
int p=checkedListBox1.SelectedIndex;
if(p==1)
{
try
{
reader = new XmlTextReader("path.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Text: //Display the text in each element.
// Console.WriteLine(reader.Value);
String s1;
s1 = reader.Value;
String ss;
ss = checkedListBox1.CheckedItems.ToString();
String sss = ss + s1;
object o3 = (object)sss;
DirectoryInfo dire = new DirectoryInfo(sss);
//DirectoryInfo[] dirs = dire.GetDirectories();
if (dire.Exists)
{
String[] folder;
folder = Directory.GetDirectories(sss);
foreach (string foldername in folder)
{
DirectoryInfo di = new DirectoryInfo(foldername);
if (di.Exists)
{
String[] files;
files = Directory.GetFiles(foldername, "*.exe*");
//FileInfo[] fi = dire.GetFiles();
foreach (String filename in files)
{
lvi = new ListViewItem();
lvi.Text = filename;
listView1.Items.Add(lvi);
}
this.Controls.Add(listView1);
}
}
}
break;
}
}
}
catch (System.Exception err)
{
MessageBox.Show("Error: " + err.Message);
}
}
|
|
|
|
|