Get error message from Command Line Parser Library - c#

I am using Command Line Parser Library for C# console application.
How I can retreive error messages if error occurs?
The use case is that this console app will be called from some another app and I want to provide to that app error message if some error occurs.
Here is a code snippet for parsing:
var options = new Options();
if (!CommandLine.Parser.Default.ParseArguments(args, options))
{
string errorMessage = ExctractParsingErrors();
GenerateErrorInvalidArguments(errorMessage);
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
}
Inside function ExctractParsingErrors() I have to extract parsing error. Example output of that function could be "-i/--input required option is missing."
Any idea how I could manage to extract parsing errors?

There is an additional overload that allows you to pass a TextWriter helpWriter, according to the documentation of the component you linked.

Related

Application crashes on element reader (pdftron)

I have an issue with pdftron, where opening a certain file, will cause our application to crash with following error:
An unhandled exception of type 'pdftron.Common.PDFNetException' occurred in PDFNet.dll
Additional information: Exception:
Message: Missing resource
Conditional expression: res
Filename : ContentResources.hpp
Function : trn::PDF::ContentResources::GetResource
Linenumber : 26
In our code: it's in the following line that the error occurs:
while ((element = elReader.Next()) != null)
When doing try/catch, we see that the only thing missing from the page is the text that's written diagonally on that page. Does this have anything to do with a missing font maybe ? Don't mind the cursor in the picture, it doesn't know where to go with the text missing.
I can send the pdf file on request.
PDF File
If you are not on the latest version of PDFNet, 6.7.1, then I would first try against that, as the issue might have been resolved already.
Otherwise, since the issue is document specific, you would need to provide that, by either sharing here, or sending to pdftron support.

OBR Repitions on HL7 ORM_o01 nHapi

Hi I am trying to parse HL7 ORM_o01 Messages but I get an error stating that I cannot get OBR repititions. I'm parsing using HL7 2.3 and this format according to the standard does allow OBR repitions. Below is the HL7 message I'm trying to parse using nHapi. Is there any way to remove the repitition limit?
MSH|^~\&|TEST|LAB|FFLEX|TEST|20030723120318||ORM^O01|163|T|2.3||||NE|NE
PID||36996-13|36996-13||WHITE^TEST^W^^|SMITH|19441215|F|NONE||1540 ECONSTITUTION^^LONG BEACH^CA^90001^^^||(480)795-3023|(480)795-3333||||00012350583|015348184||||
NTE|1||Patient Level Comment Only
PV1|1|I|ER||||10830^ATTEND^ING|20830^REF^ALICE^|30830^LEE^CONSULT^||||||||40830^LEE^ADMIT^||3501319|
DG1|1||001.9^CHOLERA NOS|
GT1|1|000614848|WHITE^TEST^W^^||1540 E CONSTITUTION^^LONGBEACH^CA^90001^^^|(505)791-1023||19441215|F||1|015348184|
IN1|1|PLAN001|210012|BANNER CHOICE PLUS|445 W 5TH PLACE #108^^LOSANGELES^CA^90002||(800)333-4444|BHA001|VALLEY MC||||||PI|WHITE^TEST^W^^|1|19441215|155 E2nd^^LONG BEACH^CA^90001^^^||||||||||||N|||||123456|
ORC|NW|000000064|||||||20030723114728|||20830^REF^ALICE^
OBR|2|000000064||ALT^ALT^L|R|20030723114734|20030723115904||4~CC|Tech|N|||20030723115904|BLDV-BLOOD VENOUS^^^LA~LEFT ARM|40830^LEE^ADMIT^|||||||||||||||285.29^ANEMIA OF OTHERCHRONIC ILLNESS (285.29)^I9||||||||||||||
NTE|1||N FASTING
DG1|1||285.29^ANEMIA OF OTHER CHRONIC ILLNESS|
OBR|2|000000064||ALB^ALBUMIN^L|R|20030723114734|20030723115904||4~CC|Tech|N|||20030723115904|BLDV-BLOOD VENOUS^^^LA~LEFT ARM|40830^LEE^ADMIT^|||||||||||||||285.29^ANEMIA OFOTHER CHRONIC ILLNESS (285.29)^I9||||||||||||||
Ok so with a bit of tinkering I found a method that works...
With the NHapi 2 core .dll files are required; NHapi.Base.dll and NHapi.Model.V23.dll
What you need to do is download the source files which you can do from sourceForge.
Open the NHapi.Model.V23 project.
In the solution the explorer expand Group then go to the ORM_O01_OBSERVATION.cs file.
inside the class constructor, refer below code:
public ORM_O01_OBSERVATION(IGroup parent, IModelClassFactory factory) : base(parent, factory){
try {
this.add(typeof(OBX), true, false);
this.add(typeof(NTE), false, true);
} catch(HL7Exception e) {
HapiLogFactory.GetHapiLog(GetType()).Error("Unexpected error creating ORM_O01_OBSERVATION - this is probably a bug in the source code generator.", e);
}
}
you need to change this.add(typeof(OBX), true, false); to read this.add(typeof(OBX), true, true);
This will allow OBX to be repeated. You should be able to apply this concept on to any field you are experiencing a problem on.

C# powershell ErrorDetail usage

I have a custom powershell command derived from PSCmdlet and on error I want to provide my own error message. As per MSDN ErrorDetails propery on ErrorRecord does that :
Provides additional error information for an error record, such as a
more detailed replacement error message.
How ever I am not able to get it working, I have DisplayStrings.resx, in this I have stored my custom error message with the ID InvalidOrNoSite, but when I execute the following code, it shows the same old style error and not my custom error.
ErrorRecord err = new ErrorRecord(new ArgumentException(), "", ErrorCategory.InvalidArgument, this);
err.ErrorDetails = new ErrorDetails(this, "Resources.DisplayStrings.ResourceManager", "InvalidOrNoSite", null);
ThrowTerminatingError(err);
Any examples on how to use ErrorDetails?
The customisation of ErrorDetail works if you change your error in a non-terminating error.
This behaviour is not explained in microsoft documentation [ or at least I have not found it :) ] but in a book I've buy some year ago ( Wrox - Professional Windows PowerShell Programming - February 2008 ) at page 97 is reported that in terminating error doesn't work.
My workaround is to set a 'flag' variable to true in the catch and after test this variable and do or not do some or all following code.

How does one lookup the error number in Windows EventLogEntry Message

I am trying to read through the Windows Event log "Error" entry messages in in c#.
foreach (EventLogEntry log in eventLog.Entries)
{
if (log.EntryType.ToString() == "Error")
{
Console.WriteLine( log.Message);
}
}
The output is "The XYZ service failed to start due to the following error: \r\n%%2"
while the entry I am looking for is
"The XYZ service failed to start due to the following error:\r\nThe system cannot find the file specified."
How does one translate from the id to the appropriate error message ?
Many thanks,
KG
Event log messages are templates, something like:
The %1 service failed to start due to the following error: \r\n%2
An event log entry contains a message number and replacement strings to substitute for %1, %2, etc.
The .Net EventLogEntry.Message property does the substitution for you, so you should never see the %1, %2, etc.
It looks like the substitution is failing, perhaps because there aren't enough replacement strings (check the EventLogEntry.ReplacementStrings property) or the format string is malformed (it seems to have a stray '%'). Though neither of these explanations seem plausible if the log entry is coming from the Service Control Manager.

Using C# how to clean up MSMQ message format to work with C++ IXMLDOMDocument2

I'm trying to get a C++ service to load an XML document from a MSMQ message generated by C#. I can't really change the C++ side of things because I'm trying to inject test messages into the queue. The C++ service is using the following to load the XML.
CComPtr<IXMLDOMDocument2> spDOM;
CComPtr<IXMLDOMNode> spNode;
CComBSTR bstrVal;
if(_FAILED(hr = spDOM.CoCreateInstance(CLSID_DOMDocument30)))
{
g_infoLog->LogCOMError(hr, "CWorker::ProcessBody() Can't Create DOM");
pWork->m_nFailure = WORKFAIL_BADXML;
goto Exit;
}
hr = spDOM->loadXML(bstrBody, &vbResult);
The C# code to send the MSMQ message looks like this (just test code not pretty):
// open the queue
var mq = new MessageQueue(destinationQueue)
{
// store message on disk at all intermediaries
DefaultPropertiesToSend = { Recoverable = true },
// set the formatter to Binary, default is XML
Formatter = new BinaryMessageFormatter()
};
// send message
mq.Send(messageContent, "TestMessage");
mq.Close();
I tried to send the same message using BinaryMessageFormatter but it puts what I think are unicode characters at the top before the XML starts.
.....ÿÿÿ
ÿ.......
......À)
If I use the default XML formatter the message has the following top element. The C++ service doesn't seem to handle this.
<?xml version="1 .0"?>..<string>& lt;
Do you know of a way I could easily clean up the unicode characters when using the binary formatter? If so I think it might work.
Have you tried the ActiveXMessageFormatter? It might not compile with it as the formatter, i have no way to test here, but it might.
EDIT: just tried and it compiles ok, whether the result is any better i still couldn't say for sure.

Categories