public static ReaderLog GetLastElements(){
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("sheet.xml"));
// normalize text representation
doc.getDocumentElement().normalize();
//System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
NodeList listOftagdata = doc.getElementsByTagName("tagdata");
int totaltagdata = listOftagdata.getLength();
System.out.println("Total no of tagdata : " + totaltagdata);
for(int s=listOftagdata.getLength()-1 ; s> listOftagdata.getLength()-2 ; s--){
Node node = listOftagdata.item(s);
if(node.getNodeType() == Node.ELEMENT_NODE){
Element element = (Element)node;
ReaderLog r = new ReaderLog() ;
//-------
NodeList nodelist = element.getElementsByTagName("tagid");
Element element1 = (Element) nodelist.item(0);
NodeList ntagid = element1.getChildNodes();
r.TagId = (ntagid.item(0)).getNodeValue() ;
//-------
NodeList nodelist2 = element.getElementsByTagName("date");
Element element2 = (Element) nodelist2.item(0);
NodeList ntagid2 = element2.getChildNodes();
r.Date = ( (ntagid2.item(0)).getNodeValue() ) ;
//-------
NodeList nodelist3 = element.getElementsByTagName("time");
Element element3 = (Element) nodelist3.item(0);
NodeList ntagid3 = element3.getChildNodes();
r.Time = ( (ntagid3.item(0)).getNodeValue() ) ;
//-------
NodeList nodelist4 = element.getElementsByTagName("antno");
Element element4 = (Element) nodelist4.item(0);
NodeList ntagid4 = element4.getChildNodes();
r.AntNo = ( (ntagid4.item(0)).getNodeValue() ) ;
//-------
NodeList nodelist5 = element.getElementsByTagName("data");
Element element5 = (Element) nodelist5.item(0);
NodeList ntagid5 = element5.getChildNodes();
r.Data = ( (ntagid5.item(0)).getNodeValue() ) ;
//-------
NodeList nodelist6 = element.getElementsByTagName("readerip");
Element element6 = (Element) nodelist6.item(0);
NodeList ntagid6 = element6.getChildNodes();
r.ReaderIP = ( (ntagid6.item(0)).getNodeValue() ) ;
//-------
NodeList nodelist7 = element.getElementsByTagName("computerdate");
Element element7 = (Element) nodelist7.item(0);
NodeList ntagid7 = element7.getChildNodes();
System.out.println( (ntagid7.item(0)).getNodeValue() ) ;
//-------
NodeList nodelist8 = element.getElementsByTagName("computertime");
Element element8 = (Element) nodelist8.item(0);
NodeList ntagid8 = element8.getChildNodes();
r.ComputerTime = ( (ntagid8.item(0)).getNodeValue() ) ;
return r ;
}//end of if clause
}//end of for loop with s var
} catch (SAXException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
}
return null ;
}
public static void RemoveLastElement() throws TransformerException, SAXException, IOException{
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc = factory.newDocumentBuilder().parse(new File("sheet.xml"));
DocumentTraversal traversal = (DocumentTraversal) doc;
Node a = doc.getDocumentElement();
NodeIterator iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true);
Element b = null;
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
Element e = (Element) n;
if ("tagdata".equals(e.getTagName())) {
b = e;
} else if ("tagdata".equals(e.getTagName()) && /*"13".equals(e.getTextContent()) && */ b != null) {
}
}
a.removeChild(b);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("sheet.xml"));
transformer.transform(source, result);
} catch (TransformerConfigurationException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (ParserConfigurationException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void AddElemet(ReaderLog r) throws TransformerException{
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("sheet.xml"));
// normalize text representation
doc.getDocumentElement().normalize();
//System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
Element newB = doc.createElement("tagdata");
Element newC = doc.createElement("tagid");
newC.setTextContent( r.TagId );
Element newD = doc.createElement("date");
newD.setTextContent( r.Date );
Element newE = doc.createElement("time");
newE.setTextContent( r.Time );
Element newF = doc.createElement("antno");
newF.setTextContent( r.AntNo );
Element newG = doc.createElement("data");
newG.setTextContent( r.Data );
Element newH = doc.createElement("readerip");
newH.setTextContent( r.ReaderIP );
Element newI = doc.createElement("computerdate");
newI.setTextContent( r.ComputerDate.toString() );
Element newJ = doc.createElement("computertime");
newJ.setTextContent( r.ComputerDate.toString() );
newB.appendChild(newC);
newB.appendChild(newD);
newB.appendChild(newE);
newB.appendChild(newF);
newB.appendChild(newG);
newB.appendChild(newH);
newB.appendChild(newI);
newB.appendChild(newJ);
doc.getDocumentElement().appendChild(newB);
doc.normalize();
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("sheet.xml"));
transformer.transform(source, result);
} catch (TransformerConfigurationException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (SAXException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
}
}
Tuesday, June 26, 2012
Sunday, June 24, 2012
Trim String by String And PadLeft Functions in java
public static String trimStringByString(String text, String trimBy) {
int beginIndex = 0;
int endIndex = text.length();
while (text.substring(beginIndex, endIndex).startsWith(trimBy)) {
beginIndex += trimBy.length();
}
while (text.substring(beginIndex, endIndex).endsWith(trimBy)) {
endIndex -= trimBy.length();
}
return text.substring(0, endIndex);
}
public static String padLeft(String in, int size, char padChar) {
if (in.length() <= size) {
char[] temp = new char[size];
/* Llenado Array con el padChar*/
for(int i =0;i<size;i++){
temp[i]= padChar;
}
int posIniTemp = size-in.length();
for(int i=0;i<in.length();i++){
temp[posIniTemp]=in.charAt(i);
posIniTemp++;
}
return new String(temp);
}
return "";
}
int beginIndex = 0;
int endIndex = text.length();
while (text.substring(beginIndex, endIndex).startsWith(trimBy)) {
beginIndex += trimBy.length();
}
while (text.substring(beginIndex, endIndex).endsWith(trimBy)) {
endIndex -= trimBy.length();
}
return text.substring(0, endIndex);
}
public static String padLeft(String in, int size, char padChar) {
if (in.length() <= size) {
char[] temp = new char[size];
/* Llenado Array con el padChar*/
for(int i =0;i<size;i++){
temp[i]= padChar;
}
int posIniTemp = size-in.length();
for(int i=0;i<in.length();i++){
temp[posIniTemp]=in.charAt(i);
posIniTemp++;
}
return new String(temp);
}
return "";
}
Monday, June 11, 2012
tsql program to kill connection sleep more than 10 min can be run in sp agent
exec sp_who
go
DECLARE @v_spid INT
DECLARE c_Users CURSOR
FAST_FORWARD FOR
SELECT SPID
FROM master..sysprocesses (NOLOCK)
WHERE spid>50
AND status='sleeping'
AND DATEDIFF(mi,last_batch,GETDATE())>=10
AND spid<>@@spid
OPEN c_Users
FETCH NEXT FROM c_Users INTO @v_spid
WHILE (@@FETCH_STATUS=0)
BEGIN
PRINT 'KILLing '+CONVERT(VARCHAR,@v_spid)+'...'
EXEC('KILL '+@v_spid)
FETCH NEXT FROM c_Users INTO @v_spid
END
CLOSE c_Users
DEALLOCATE c_Users
see also
http://support.microsoft.com/kb/137983
go
DECLARE @v_spid INT
DECLARE c_Users CURSOR
FAST_FORWARD FOR
SELECT SPID
FROM master..sysprocesses (NOLOCK)
WHERE spid>50
AND status='sleeping'
AND DATEDIFF(mi,last_batch,GETDATE())>=10
AND spid<>@@spid
OPEN c_Users
FETCH NEXT FROM c_Users INTO @v_spid
WHILE (@@FETCH_STATUS=0)
BEGIN
PRINT 'KILLing '+CONVERT(VARCHAR,@v_spid)+'...'
EXEC('KILL '+@v_spid)
FETCH NEXT FROM c_Users INTO @v_spid
END
CLOSE c_Users
DEALLOCATE c_Users
see also
http://support.microsoft.com/kb/137983
Sunday, April 22, 2012
Logging Unhandled Exceptions in an ASP.NET Website
When developing an ASP.NET application, one of the routine tasks is to plan for unhandled exceptions. If theexception is left unhandled, by default, the ASP.NET runtime will either display a nasty error message to your user or it will show the exception details page, depending on how you configure your website. Allowing the user to see a nasty error messages is far from recommended (see this article for exception handling best practices). If you simply show a generic exception details page, you will lose valuable information you may need to debug the issue.
For a few years, I have been using this strategy for processing unhandled exceptions in my websites. When an unhandled exception occurs, the strategy is to:
- Catch the error in the application-level Error event handler (Global.asax).
- "Process" the error by sending an email containing the exception details.
- Redirect to a custom webpage displaying a user-friendly message to the end user.
Now, I am not suggesting that you should not use
try catch
blocks in your code to catch errors where you can recover. This is a strategy for catching those unexpected errors in one central location, logging the exceptioninformation, sending an email to a developer, and displaying a friendly message to your user.
The problem with the approach I have been using is that it leaves the logging and sending of an email up to you to manually implement. In fact, I was only sending out an email with the error message and relying on the server's application logs to record the errors. Tracking down the problem, who the user was, etc., became a real pain using only an email message and application logs. I wished for an easier way to receive a notice of an error and a way to store the information so that I could run a report to view the errors or even see some statistics.
So, I started doing some research on the Microsoft Enterprise Application blocks, and thought that they were exactly what I was looking for. This article in particular was very helpful, and you may want to follow this approach for your N-Tiered applications.
What I love about using the application blocks is that you simply enter a few settings in the web.config file, add references to the application blocks, and with a few lines of code, I was sending emails and logging exceptions to SQL Server. Next, one quick Reporting Services report later, and I could easily view the information in the Logging database!
Using the code
What you will need to use this code:
- MS Enterprise Library 5.0
- Must be using at least .NET Framework 3.5
- SQL Server - with the Enterprise Library Logging database installed
From the MS Enterprise Library documentation:
"In the folder where you installed the Enterprise Library, open the subfolderSource\Blocks\Logging\Src\DatabaseTraceListener\Scripts in Windows Explorer. This folder contains a SQL script named LoggingDatabase.sql and a command file named CreateLoggingDb.cmd that executes this script. The script creates a new database named Logging, and adds to it all of the tables and Stored Procedures required by the database trace listener to write log entries to a database."
Add references to the following application blocks (should be located at C:\Program Files (x86)\Microsoft EnterpriseLibrary 5.0\Bin\):
- Microsoft.Practices.EnterpriseLibrary.Common.dll
- Microsoft.Practices.EnterpriseLibrary.Data.dll
- Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
- Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll
- Microsoft.Practices.EnterpriseLibrary.Logging.dll
- Microsoft.Practices.EnterpriseLibrary.Logging.Database.dll
After you install the MS Enterprise Library, when you right click on the web.config file in Visual Studio, you will see a menu option for Edit Enterprise Library V5 Configuration. You can use this tool to enter the necessary configuration info, or you can do it by manually updating the web.config file. I strongly recommend the former especially if you are new to using the application blocks. For the purposes of this article, under the Blocks menu, choose Add Logging Settings and Add Exception Handling Settings. By default, you should already have Application Settings and Database Settings. After you have those added, you can close and save the tool, which will update yourweb.config file.
For simplicity, update the web.config with this code snippet. You can then go back to the tool to more easily see the settings. Make sure to update the email addresses and STMP server in the Logging Settings and the connection string for the Logging database under Database Settings.
The snippet below has been wrapped to avoid page scrolling.
<configsections />
<section name="loggingConfiguration" requirepermission="true"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<section name="exceptionHandling" requirepermission="true"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.
Configuration.ExceptionHandlingSettings,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling,
Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
...
</configsections />
<loggingconfiguration tracingenabled="true"
defaultcategory="General" />
<listeners />
<add name="Email Trace Listener"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.
EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging,
Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerdatatype="Microsoft.Practices.EnterpriseLibrary.Logging.
Configuration.EmailTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging,
Version=5.0.414.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"
toaddress="someone@yourcompany.com"
fromaddress="noreply@yourcompany.com"
subjectlineender="- Log Example Website"
smtpserver="YourSMTPServer"
formatter="Email Text Formatter"
traceoutputoptions="DateTime, Timestamp, ProcessId, Callstack" />
<add name="Database Trace Listener"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.
FormattedDatabaseTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging.Database,
Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerdatatype="Microsoft.Practices.EnterpriseLibrary.Logging.
Database.Configuration.FormattedDatabaseTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging.Database,
Version=5.0.414.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"
formatter="Text Formatter"
traceoutputoptions="LogicalOperationStack, DateTime,
Timestamp, ProcessId, ThreadId, Callstack"
databaseinstancename="LoggingConnectionString"
writelogstoredprocname="WriteLog"
addcategorystoredprocname="AddCategory" />
</listeners />
<formatters />
<add name="Text Formatter"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter,
Microsoft.Practices.EnterpriseLibrary.Logging,
Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="Application Name: Log Example Website
Message: {message}{newline}
Category: {category}{newline}
Extended Properties: {dictionary({key} - {value}{newline})}" />
<add name="Email Text Formatter"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="There was an error. Please see the Logging database
for more information. Timestamp: {timestamp(local)}
{newline} Message: {message}{newline}" />
</formatters />
<categorysources />
<add name="ErrorLogging" switchvalue="All" />
<listeners />
<add name="Email Trace Listener" />
<add name="Database Trace Listener" />
</listeners />
</add />
</categorysources />
<specialsources />
<allevents name="All Events" switchvalue="All" />
<notprocessed name="Unprocessed Category" switchvalue="All" />
<errors name="Logging Errors & Warnings" switchvalue="All" />
<listeners />
<add name="Email Trace Listener" />
<add name="Database Trace Listener" />
</listeners />
</errors />
</specialsources />
</loggingconfiguration />
<exceptionhandling />
<exceptionpolicies />
<add name="AllExceptionsPolicy" />
<exceptiontypes />
<add name="All Exceptions"
type="System.Exception, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
posthandlingaction="None" />
<exceptionhandlers />
<add title="Enterprise Library Exception Handling"
name="AllExceptionsLoggingHandler"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.
Logging.LoggingExceptionHandler, Microsoft.Practices.
EnterpriseLibrary.ExceptionHandling.Logging,
Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
logcategory="ErrorLogging"
eventid="100" severity="Error"
formattertype="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.
TextExceptionFormatter,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
priority="0" />
</exceptionhandlers />
</add />
</exceptiontypes />
</add />
</exceptionpolicies />
</exceptionhandling />
<connectionstrings />
<add name="LoggingConnectionString"
connectionstring="Data Source=YourSQLServer;
Initial Catalog=Logging;
Integrated Security=SSPI"
providername="System.Data.SqlClient" />
</connectionstrings />
Place this code in the Global.asax file:
protected void Application_Error(object sender, EventArgs e)
{
//uncomment in order to bypass logging when running locally.
//if (!Request.IsLocal)
//{
Exception ex = Server.GetLastError();
if (ex is HttpUnhandledException && ex.InnerException != null)
{
ex = ex.InnerException;
}
if (ex != null)
{
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.
ExceptionPolicy.HandleException(ex, "AllExceptionsPolicy");
Server.ClearError();
Response.Redirect("~/Utility/ErrorPage.htm");
}
//}
}
As you can see from the code above, after the error is handled by the application block, the user will be redirected to the ~/Utility/ErrorPage.htm page. So, you will need to create a page for your site, or you can use the one in thesample code for download in this article.
So, that is all there is to using the MS Enterprise Application Blocks to process unhandled exceptions in an ASP.NET website. Download the sample code, which contains a project to demo how to process unhandled exceptions. If you are familiar with SQL Server Reporting Services, you can create your own custom reports to view the data logged to SQL Server.
Subscribe to:
Posts (Atom)